Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YuMp's avatar
Level 2

overtrue Laravel Follow Problem

Hello everyone, hope you are having a great day. My problem is the following image and followers username is displayed correctly count is also correct. My problem is the "followings" does not display neither the image nor the username. the count is correct.

I am using overtrue Laravel Follow, and I'm not able to make the correct listing of the followings, does anyone have any idea how I might be resolving this?

Thank you very much in advance, attached is an image and code below.

Any help is welcome. IMG: https://i.ibb.co/GJdry89/following-no-show-image.png User Model:

use Overtrue\LaravelFollow\Traits\Follower; use Overtrue\LaravelFollow\Traits\Followable; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, Follower, Followable;

HomeController

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

    $posts = Post::where('user_id', $user->id)->simplePaginate(3);
    $user = User::find($user->id);
    $followings = $user->followings()->with('followable')->get();

    dd($followings);
    return view('/@', compact('user'), compact('posts'), compact('followings'));

}

blade view

Followers {{ $user->followers()->get()->count() }} @include('userList', ['users'=>$user->followers()->paginate(1)])
           </div>
       </div>
       <div class="card-body">
           <nav>
               <div class="nav nav-tabs" id="nav-tab" role="tablist">

                   <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#following" role="tab" aria-controls="nav-profile" aria-selected="false">Following <span class="badge badge-primary">{{ $user->followings()->get()->count() }}</span></a>
               </div>
           </nav>

       </div>
       <div class="tab-pane fade" id="following" role="tabpanel" aria-labelledby="nav-profile-tab">
           <div class="row pl-5">
               @include('userList', ['users'=>$user->followings()->paginate(1)])
           </div>
       </div>
   </div>

UPDATE --->

Hi hope ALL you are having a great day.

I tried to apply the solution, but it does not return an object. How can I get "followings" that return an object, as with "followers"?

Thanks in advance follow my code below, any help is welcome.

returns this --> [{"id":60,"user_id":5,"followable_type":"App\Models\User","followable_id":9,"accepted_at":"2022-12-20T20:36:49.000000Z","created_at":"2022-12-20T20:36:49.000000Z","updated_at":"2022-12-20T20:36:49.000000Z"}]

code -->

`

@include('userList', ['users'=>$user->followers()->paginate(1)])

@include('userList', ['users'=>$user->followings()->paginate(1)])

@foreach($following as $following)

{{$following->id}}

@endforeach

UPDATE AGAIN USING DD dd($followings); Return right data. #attributes: array:7 [▼ "id" => 60 "user_id" => 5 "followable_type" => "App\Models\User" "followable_id" => 9 "accepted_at" => "2022-12-20 20:36:49" "created_at" => "2022-12-20 20:36:49" "updated_at" => "2022-12-20 20:36:49"

but I have problems in my view, I can't perform the foreach, I get the following error. Undefined variable $followings

if possible, visit my repository created specifically for this issue, where I have uploaded all files related to this issue. Thank you very much in advance. sorry to bother https://github.com/applive3/laravelfollowingproblem

0 likes
3 replies
Sinnbeck's avatar

You are getting the user from the database twice. No need for that.

1 like
YuMp's avatar
YuMp
OP
Best Answer
Level 2

Solved using foreach.

Solution code example-->

@foreach($followings as $following)

{{ $following->followable->username }} {{ $following->created_at }}

@endforeach

1 like

Please or to participate in this conversation.