我已经为一个大学项目建立了一个基本的laravel社交媒体网站,虽然其中很多都在运作,但我对朋友表的不满存在问题。我可以让朋友循环显示,但由于某种原因,它只会反复显示用户个人资料页面名称,这取决于他们在数据库中的朋友数量,我已多次通过代码但是只是看不出原因。我的代码低于
我的代码在下面:
User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'users';
protected $fillable = [
'id', 'username','firstName', 'lastName', 'bio', 'DoB', 'homeLocation', 'currentLocation', 'journeys', 'friends', 'email', 'password',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function getName()
{
if ($this->firstName && $this->lastName) {
return "{$this->firstName} {$this->lastName}";
}
if ($this->firstName) {
return $this->firstName;
}
return null;
}
public function getNameOrUsername()
{
return $this->getName() ?: $this->username;
}
public function getFirstNameOrUsername()
{
return $this->firstName ?: $this->username;
}
public function getAvatarUrl()
{
return "https://www.gravatar.com/avatar/{{ md5($this->email) }}?d=mm";
}
public function statuses()
{
return $this->hasMany('App\Posts', 'user_id');
}
public function friendsOfMine()
{
return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_id');
}
public function friendsOf()
{
return $this->belongsToMany('App\User', 'friends', 'friend_id', 'user_id');
}
public function friends()
{
return $this->friendsOfMine()->wherePivot('accepted', true)->get()->merge($this->friendsOf()->wherePivot('accepted', true)->get());
}
public function friendRequests()
{
return $this->friendsOfMine()->wherePivot('accepted', false)->get();
}
public function friendRequestsPending()
{
return $this->friendsOf()->wherePivot('accepted', false)->get();
}
public function hasFriendRequestPending(User $users)
{
return (bool) $this->friendRequestsPending()->where('id', $users->id)->count();
}
public function hasFriendRequestReceived(User $users)
{
return (bool) $this->friendRequests()->where('id', $users->id)->count();
}
public function addFriend(User $users)
{
$this->friendsOf()->attach($users->id);
}
public function acceptFriendRequest(User $users)
{
$this->friendRequests()->where('id', $users->id)->first()->pivot->update([
'accepted' => true,
]);
}
public function isFriendsWith(User $users)
{
return (bool) $this->friends()->where('id', $users->id)->count();
}
}
ProfileController.php
<?php
namespace App\Http\Controllers;
use App\Posts;
use App\User;
use Auth;
use Validator;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class ProfileController extends BaseController
{
public function getProfile($username)
{
$users = User::where('username', $username)->first();
if (!$users) {
abort(404);
}
$statuses = $users->statuses()->get();
return view('profile/index')
->with('users', $users)
->with('statuses', $statuses)
->with('authUserIsFriend', Auth::user()->isFriendsWith($users));
}
public function getEdit()
{
return view('profile/edit');
}
public function postEdit(Request $request)
{
Auth::user()->update([
'username' => $request->input('username'),
'firstName' => $request->input('firstName'),
'lastName' => $request->input('lastName'),
'bio' => $request->input('bio'),
'DoB' => $request->input('DoB'),
'homeLocation' => $request->input('homeLocation'),
'currentLocation' => $request->input('currentLocation'),
'email' => $request->input('email'),
]);
return redirect()
->route('profile/edit')
->with('info', 'Your profile has been updated');
}
}
FriendController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
class FriendController extends BaseController
{
public function getIndex()
{
$friends = Auth::user()->friends();
$requests = Auth::user()->friendRequests();
return view('users/friends/index')
->with('friends', $friends)
->with('requests', $requests);
}
public function getAdd($username)
{
$users = User::where('username', $username)->first();
if (!$users)
{
return redirect()->route('/home')
->with('info', 'That user could not be found');
}
if (Auth::user()->id === $users->id)
{
return redirect()->route('/home');
}
if (Auth::user()->hasFriendRequestPending($users) || $users->hasFriendRequestPending(Auth::user()))
{
return redirect()->route('profile/index', ['username' => $users->username])
->with('info', 'Friend Request Already Waiting');
}
if (Auth::user()->isFriendsWith($users))
{
return redirect()->route('profile/index', ['username' => $users->username])
->with('info', 'Your are already friends');
}
Auth::user()->addFriend($users);
return redirect()->route('profile/index', [ 'username' => $username])
->with('info', 'Friend Request Sent');
}
public function getAccept($username)
{
$users = User::where('username', $username)->first();
if (!$users)
{
return redirect()->route('/home')
->with('info', 'That user could not be found');
}
if (!Auth::user()->hasFriendRequestReceived($users))
{
return redirect()->route('/home');
}
Auth::user()->acceptFriendRequest($users);
return redirect()->route('profile/index', ['username' => $username])
->with('info', 'Friend Request Accepted');
}
}
userblock.blade.php
<div class="media">
<a class="pull-left" href="{{ route('profile/index', ['username' => $users->username]) }}">
<img class="media-object" alt="{{ $users->getNameOrUsername() }}" src="{{ $users->getAvatarUrl() }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="{{ route('profile/index', ['username' => $users->username]) }}">{{ $users->getNameOrUsername() }}</a></h4>
</div>
@if ($users->currentLocation)
<p>{{ $users->currentLocation }}</p>
@endif
用户/朋友/ index.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-6">
<h3>Your Friends</h3>
@if (!$friends->count())
<p>Your List is Empty</p>
@else
@foreach ($friends as $users)
@include('users/partials/userblock')
@endforeach
@endif
</div>
<div class="col-lg-6">
<h3>Requests</h3>
@if (!$requests->count())
<p>You Have No Requests</p>
@else
@foreach ($requests as $users)
@include('users/partials/userblock')
@endforeach
@endif
</div>
</div>
@endsection