如何在 Laravel 5.8 中获得关注用户的帖子
How to get following users posts in Laravel 5.8
我的 Laravel 5.8 项目中有两个模型,两个模型中的关系如下所示 类。我怎样才能只使用一个 sql 查询来获取与我关注的每个用户相关的每一个 post 记录?我可以使用 Eloquent Query Builder 获取它还是我需要一个 Raw SQL Query ?有人可以告诉我 SQL 查询吗?
抱歉,我不知道问题的标题。
提前致谢!
用户Class.
class User extends Authenticatable implements MustVerifyEmail{
use Notifiable, MessageAccessible, TagsCreator;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
"lastname",
"country",
"city",
"phone_number",
'e_mail',
'password',
"role_id",
"profile_picture",
"occupation",
"biography"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['email_verified_at' => 'datetime'];
public function posts(){
return $this->hasMany(Post::class);
}
public function followers(){
return $this->belongsToMany(User::class, 'follower_followed', 'followed_id', 'follower_id');
}
public function following(){
return $this->belongsToMany(User::class, 'follower_followed', 'follower_id', 'followed_id');
}
}
Post Class.
class Post extends Model{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
"post_permission_id",
"title",
"content",
"likes",
"dislikes"
];
public function user(){
return $this->belongsTo(User::class);
}
}
在 finding/selecting 用户之后,你可以通过这样的方式获取相关的 posts:
$user = Auth::user(); // selecting the logged in user
$user->posts;
为此,您必须在 posts
table 中包含 user_id
列。
如果你想拥有所有用户和他们的 post,你可以这样做:
$usersWithPosts = User::with('posts')->get();
这将 return 所有用户(无论他们是否有任何 post),如果您只希望基本上至少有一个 post 的用户执行此操作:
$usersWithPosts = User::has('posts')->get();
我想你需要做的就是这个。
不确定,但在某些 Laravel 版本中,您必须在此处使用 select
而不是 pluck
。此 defo 适用于 5.6
$posts = Post::whereIn('user_id', User::find($user_id)->followers->pluck('id'))->get();
那么您可能想按关注者排序帖子
$posts = Post::whereIn('user_id', User::find($user_id)->followers->pluck('id'))->orderBy('user_id', 'ASC')->get();
这是 whereIn 的文档向下滚动一点,whereIn 上没有直接锚点 :-)
我的 Laravel 5.8 项目中有两个模型,两个模型中的关系如下所示 类。我怎样才能只使用一个 sql 查询来获取与我关注的每个用户相关的每一个 post 记录?我可以使用 Eloquent Query Builder 获取它还是我需要一个 Raw SQL Query ?有人可以告诉我 SQL 查询吗?
抱歉,我不知道问题的标题。
提前致谢!
用户Class.
class User extends Authenticatable implements MustVerifyEmail{
use Notifiable, MessageAccessible, TagsCreator;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
"lastname",
"country",
"city",
"phone_number",
'e_mail',
'password',
"role_id",
"profile_picture",
"occupation",
"biography"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['email_verified_at' => 'datetime'];
public function posts(){
return $this->hasMany(Post::class);
}
public function followers(){
return $this->belongsToMany(User::class, 'follower_followed', 'followed_id', 'follower_id');
}
public function following(){
return $this->belongsToMany(User::class, 'follower_followed', 'follower_id', 'followed_id');
}
}
Post Class.
class Post extends Model{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
"post_permission_id",
"title",
"content",
"likes",
"dislikes"
];
public function user(){
return $this->belongsTo(User::class);
}
}
在 finding/selecting 用户之后,你可以通过这样的方式获取相关的 posts:
$user = Auth::user(); // selecting the logged in user
$user->posts;
为此,您必须在 posts
table 中包含 user_id
列。
如果你想拥有所有用户和他们的 post,你可以这样做:
$usersWithPosts = User::with('posts')->get();
这将 return 所有用户(无论他们是否有任何 post),如果您只希望基本上至少有一个 post 的用户执行此操作:
$usersWithPosts = User::has('posts')->get();
我想你需要做的就是这个。
不确定,但在某些 Laravel 版本中,您必须在此处使用 select
而不是 pluck
。此 defo 适用于 5.6
$posts = Post::whereIn('user_id', User::find($user_id)->followers->pluck('id'))->get();
那么您可能想按关注者排序帖子
$posts = Post::whereIn('user_id', User::find($user_id)->followers->pluck('id'))->orderBy('user_id', 'ASC')->get();
这是 whereIn 的文档向下滚动一点,whereIn 上没有直接锚点 :-)