hootlex / laravel-friendships

This package gives Eloquent models the ability to manage their friendships.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getAcceptedFriends not returning the user details

antweny opened this issue · comments

When i fetch all accepted friendship and pass to the view the names and id of the friends not showing.

My controller file

public function connections ()
        {
            $user = Auth::user();
            $connections = $user->getAcceptedFriendships();;
            return view('users.friendships.connections')->with(compact('connections'));
        }

View file

@foreach($connections as $user)
     <li>
        <h6>{{$user->name}}</h6>
    </li>
@endforeach

It displays the list of all accepted friendship without their details like their name.

I need support

Hey, since the pivot table only holds the id's, you will only get the collection of id's when you call the function as is. If you want to get the full details of the users like name, email, etc then you need to eager load them by using ->load('sender') or ->load('recipient')

public function connections ()
        {
            $user = Auth::user();
            $connections = $user->getAcceptedFriendships()->load('sender')->load('recipient');
            return view('users.friendships.connections')->with(compact('connections'));
        }

the above will load the user details for both the sender and the recipient user, if you want details of just one of them, then just use load() with that one.

-TT

I tried this but still details not showing on the view

Can you show me your view file?

here it is

@extends('layouts.templates.twoColumns')

@section('title') My Network | TPE @endsection

@section('content')
    @include('layouts.partials.alerts')
    @if(isset($connections) && !empty($connections))
        <div class="card connections">
            <div class="card-header">
                <h4>{{$connections->count()}} Connections</h4>
            </div>
            <div class="card-body" style="padding:10px;">
                <ul>
                    @foreach($connections as $user)
                        <li>
                            <span>{{$user->name}}</span>
                            <span>{{$user->email}}</span>
                        </li>
                    @endforeach
                </ul>
            </div>
        </div>
    @else
        <div class="card">
            <div class="card-body" style="padding: 10px;">
                <h6 class="text-center">No pending Invitations</h6>
            </div>
        </div>
    @endif
@endsection

Okay, the thing is when you are eager loading properties, you need to echo them accordingly. Let me show you just for sender -

In your controller -
public function connections ()
        {
            $user = Auth::user();
            $connections = $user->getAcceptedFriendships()->load('sender');
            return view('users.friendships.connections')->with(compact('connections'));
        }
--------------------------------------
In your view -
 @foreach($connections as $connection)
      <li>
            <span>{{$connection->sender->name}}</span>
             <span>{{$connection->sender->email}}</span>
      </li>
 @endforeach

And you do the same thing with the recipient or any other eager loaded property.

Next time dd your collections to see all the properties associated with it by doing dd($connections); in your controller.

-TT

this solution works fine but sometimes a sender/recipient is an logged user how to handle this

tried this and worked fine

@foreach($connections as $user)
                        @if($user->sender->name != Auth::user()->name)
                            <li>
                                <div class="float-left">
                                    <a href="{{route('profile.show',$user->id)}}">
                                        <div>
                                            @if (!empty($user->sender->user_detail->profile_picture) )
                                                <img src="{{asset('/uploads/profiles/'.$user->sender->user_detail->profile_picture)}}" />
                                            @else
                                                <img src="{{asset('/uploads/profiles/avatar.jpeg')}}" />
                                            @endif
                                        </div>
                                        <div><h6>{{$user->sender->name}}</h6></div>
                                    </a>
                                </div>
                                <div class="float-right" style="padding-top:10px;">
                                    <a class="btn btn-outline-dark btn-sm" href="{{route('friendships.accept',$user->sender->id)}}"><i class="fa fa-envelope"></i> Message</a>
                                    <a class="btn btn-outline-danger btn-sm" href="{{route('friendships.accept',$user->sender->id)}}" title="Remove Friend"><i class="fa fa-times"></i></a>
                                </div>
                            </li>
                        @elseif($user->recipient->name != Auth::user()->name)
                            <li><div class="float-left">
                                    <a href="{{route('profile.show',$user->id)}}">
                                        <div>
                                            @if (!empty($user->recipient->user_detail->profile_picture) )
                                                <img src="{{asset('/uploads/profiles/'.$user->recipient->user_detail->profile_picture)}}" />
                                            @else
                                                <img src="{{asset('/uploads/profiles/avatar.jpeg')}}" />
                                            @endif
                                        </div>
                                        <div><h6>{{$user->recipient->name}}</h6></div>
                                    </a>
                                </div>
                                <div class="float-right" style="padding-top:10px;">
                                    <a class="btn btn-outline-dark btn-sm" href="{{route('friendships.accept',$user->recipient->id)}}"><i class="fa fa-envelope"></i> Message</a>
                                    <a class="btn btn-outline-danger btn-sm" href="{{route('friendships.accept',$user->recipient->id)}}" title="Remove Friend"><i class="fa fa-times"></i></a>
                                </div>
                            </li>
                        @endif
                    @endforeach