YaSh Chaudhary YaSh Chaudhary - 3 months ago 11
PHP Question

Friends system bug (laravel 5.2)(not getting desired view)

when two users become friend, it is shown

you and

{{$user->getNameOrUsername()}}
are friends (working fine)

but it is shown on every profile and I want that it is only shown on those two users (which are friends)

if you need any other file or function ,tell me.

my view :

@if(Auth::user()->hasFriendRequestPending($user))
<p>waiting for {{$user->getNameOrUsername()}} to accept ur reqst </p>
@elseif(Auth::user()->hasFriendRequestReceived($user))
<a href="{{route('friend.accept',['username' => $user->username])}}" class="btn btn-primary" >Accept frnd reqst</a>
@elseif(Auth::user()->isFriendsWith($user))
<p>you and {{$user->getNameOrUsername()}} are friends</p>
@else
<a href="{{route('friend.add',['username' => $user->username])}}" class="btn btn-primary">Add as friend</a>
@endif


friend controller :

class FriendController extends Controller
{
public function getIndex(){

$friends=Auth::user()->friends();
$requests = Auth::user()->friendRequests();
return view('friends',['friends'=>$friends,'requests' => $requests]);
}

public function getAdd($username){


$user = User::where('username', $username)->first();
if(!$user){
return redirect()->route('home')->with('info', 'That user could not be found');
}
if(Auth::user()->id === $user->id){
return redirect()->route('home');
}
if(Auth::user()->hasFriendRequestPending($user) || $user->hasFriendRequestPending(Auth::user())) {
return redirect()->route('myplace', ['username' => $user->username])
->with('info', 'Friend Request already pending.');
}
if(Auth::user()->isFriendsWith($user)) {
return redirect()->route('myplace', ['username' => $user->username])
->with('info', 'You are already friends.');
}
Auth::user()->addFriend($user);
return redirect()->route('myplace', ['username' => $user->username])
->with('info', 'Friend Request Sent.');
}

public function getAccept($username){
$user = User::where('username', $username)->first();

if(!$user) {
return redirect()->route('home')->with('info', 'That user can not be found.');
}
if(!Auth::user()->hasFriendRequestReceived($user)) {
return redirect()->route('home');
}
Auth::user()->acceptFriendRequest($user);
return redirect()->route('myplace', ['username' => $user->username])
->with('info', 'Friend Request accepted.');
}
}


postcontroller:

public function getmyplace($username)
{
$user = User::where('username', $username)->first();
if(!$user)
abort(404);

$posts=Post::where(function($query) use($user)
{
$query->where('user_id',$user->id)
->orWhere('target_id',$user->id);
})
->orderBy('created_at','desc')->get();


//User::find(Auth::id())->posts;

//$posts=Post::orderBy('created_at','desc')->get();

$accounts=Account::orderBy('updated_at','desc')->limit(1)->get();

return view('myplace',['user'=>$user,'posts'=>$posts,'accounts'=>$accounts]);
}


user model functions:

public function getNameOrUsername(){
return $this->getName() ?: $this->username;
}

public function getFirstNameOrUsername() {
return $this->firstname ?: $this->username;
}
public function friendsOfMine(){
return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_id');
}
public function friendOf(){
return $this->belongsToMany('App\User', 'friends', 'friend_id', 'user_id');
}
public function friends(){
return $this->friendsOfMine()->wherePivot('accepted', true)->get()
->merge($this->friendOf()->wherePivot('accepted', true)->get());
}
public function friendRequests(){
return $this->friendsOfMine()->wherePivot('accepted', false)->get();
}
public function friendRequestsPending() {
return $this->friendOf()->wherePivot('accepted', false)->get();
}
public function hasFriendRequestPending(User $user) {
return (bool) $this->friendRequestsPending()->where('id', $user->id)->count();
}
public function hasFriendRequestReceived(User $user){
return (bool) $this->friendRequests()->where('id', $user->id)->count();
}
public function addFriend(User $user){
$this->friendOf()->attach($user->id);
}

public function deleteFriend(User $user){
$this->friendOf()->detach($user->id);
}

public function acceptFriendRequest(User $user) {
$this->friendRequests()->where('id', $user->id)->first()->pivot
->update([
'accepted'=>true,
]);
}
public function isFriendsWith(User $user) {

return (bool) $this->friends()->where('id',$user->id)->count();
}

Answer

As discussed in the chat http://chat.stackoverflow.com/rooms/121812 the problem is that you return $user from the controller

 return view('myplace',['user'=>$user,'posts'=>$posts,'accounts'=>$accounts]);

And then later within the view loop through a userse friends

@foreach(Auth::user()->friends() as $user) {

}

By this the $user variable returned from the controller is overwritten.

Simply renaming $user resolves the problem

Comments