php - Making a followers system

431

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
            Auth::user()->follows()->create([
                '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()
    {
      $usuarioLog=Auth::user();

      $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();
      $usuarioLog=Auth::user();
      $myBooks=Book::where("user_id","=","$usuarioLog->id")->get();



      $vacLibros=compact("userBooks");
      $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>

   @empty
   <p> No sigues a nadie</p>
   @endforelse
939

Answer

Solution:

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', 'users.id', '=', 'follow.target_id')
->select('follow.*','users.*')->get();

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

Source

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.