php - Making a followers system


I am trying to make an app with followers and I am having some trouble to see in the view the people that i am following

this is the user model :

public function books(){
      return $this->hasMany('App\Book', 'book_id');

    public function follows() {
        return $this->hasMany(Follow::class);
    public function isFollowing($target_id)
        return (bool)$this->follows()->where('target_id', $target_id)->first(['id']);

and this is the follow model

class Follow extends Model
  protected $fillable = ['target_id'];

    public function user()
        return $this->belongsTo(User::class);

this is the FollowController

public function follow(User $user)
        if (!Auth::user()->isFollowing($user->id)) {
            // Create a new follow instance for the authenticated user
                'target_id' => $user->id,

            return back()->with('success', 'You are now friends with '. $user->name);
        } else {
            return back()->with('error', 'You are already following this person');


and this is the userController

public function showOwnProfile()

      $user = User::find($usuarioLog->id);
      $follow=Follow::where("target_id","=",$usuarioLog["id"] )->get();
      $following=Follow::where("user_id","=",$usuarioLog["id"] )->get();

      $userBooks = Book::where('user_id', '=', $usuarioLog["id"])->get();

      $vacUser = compact('user', 'follow','following','myBooks','usuarioLog');

      return view("/profile", $vacLibros, $vacUser);

and finally the view (where I want to visualize the people that i am following)

<h1>lista de seguidos</h1>

   @forelse ($following as $following_user) //??????

   <p> <a class="page_speed_1999944662"  href="/normalProfile/{{$following_user}}"> {{//?????}}</a></p>

   <p> No sigues a nadie</p>



You need to put the column in the{{}} put what you're calling column here

<p> <a class="page_speed_1999944662" href="/normalProfile/{{$following_user->column}}"> {{//?????}}</a></p>

Since I don't know what your column name is I just put column so you edit that one.

I'm not knowledgable in relationship query but try this:

$following=Follow::where("user_id","=",$usuarioLog["id"] )
->join('users', '', '=', 'follow.target_id')

People are also looking for solutions to the problem: Get Multiple Values with comma separated Using PHP and JavaScript


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.