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
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
Answer
Solution:
You need to put the column in the
{{}}
put what you're calling column hereSince 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: