I have a follow users system in place for my application written in Laravel and I am trying to retrieve data from my DB based on certain followers. It's a recipe application with a users, recipes, and followers table. I am currently retrieving a users followers by writing $user->followers. However, I want to set up my relationship in such a way so that I can retrieve the particular recipes that belong to a given users followers. Something along the lines of $user->followers->recipes. I don't have the correct logic or Eloquent relationships in place to do that right now. Any suggestions on how that might get done?
User Model
public function followers(){
return $this->belongsToMany(User::class, 'followers', 'leader_id', 'follower_id')->withTimestamps();
}
public function followings(){
return $this->belongsToMany(User::class, 'followers', 'follower_id', 'leader_id')->withTimestamps();
}
Recipe Model
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
public function comments(){
return $this->hasMany('App\Comment');
}
public function likes(){
return $this->hasMany('App\Like');
}
FollowerController
class FollowerController extends Controller
{
public function followUser($id){
$user = User::find($id);
if(!$user){
return redirect()->back()->with('error', 'User does not exist.');
}
$user->followers()->attach(auth()->user()->id);
return redirect()->back()->with('success', 'You now follow the user!');
}
public function unfollowUser($id){
$user = User::find($id);
if(!$user){
return redirect()->back()->with('error', 'User does not exist.');
}
$user->followers()->detach(auth()->user()->id);
return redirect()->back()->with('success', 'You unfollowed the user!');
}
public function show($id){
$user = User::find($id);
$followers = $user->followers;
$followings = $user->followings;
return view('welcome')->with('user', $user);
}
Is there any way I can setup a relationship between models User, Recipe, and Follow? What would be the best way to go about retrieving any information pertaining to the particular set of followers of a user? Let me know if I need to share anymore code with you guys! Thanks for the help!
What you need is a HasManyThrough
relationship.
In your User model:
public function recipes()
{
return $this->hasManyThrough(
'App\Recipe',
'App\User',
'leader_id',
'user_id',
'id',
'id'
);
}
Complete documentation (you may need to better set some foreign keys) can be found at: https://laravel.com/docs/5.8/eloquent-relationships#has-many-through .
EDIT: I'm wrong. Your followers
relationship is a many-to-many, not a one-to-many, so this method is not gonna work. I will leave here the comment in case someone has trouble with a one-to-many relation.
You can define a direct relationship by "skipping" the users
table:
class User extends Model
{
public function followerRecipes() {
return $this->belongsToMany(
Recipe::class, 'followers', 'leader_id', 'follower_id', null, 'user_id'
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.