【问题标题】:How to get following users posts in Laravel 5.8如何在 Laravel 5.8 中获取关注用户的帖子
【发布时间】:2020-05-11 14:42:52
【问题描述】:

我的 Laravel 5.8 项目中有两个模型,两个模型类中的关系如下所示。如何使用单个 sql 查询获取与我关注的每个用户相关的每个帖子记录?我可以使用 Eloquent Query Builder 还是我需要 Raw SQL Query 来获得它?有人可以告诉我执行此操作的 SQL 查询吗?

抱歉,我不知道该问题的标题。

提前致谢!

用户类。

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');
   }
}

发布类。

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);
   }
}

【问题讨论】:

    标签: sql laravel eloquent laravel-5.8 laravel-query-builder


    【解决方案1】:

    我认为你需要做的是这个。

    不确定,但在某些 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 上没有直接锚点:-)

    【讨论】:

    • 非常感谢。可以的,能不能告诉我用什么sql语句来实现呢。
    • 什么意思?顺便说一句...在 Laravel 5.6 中,pluck() 在 5.8 中不确定
    • 我如何使用原始 SQL 查询来做到这一点?
    • 你可以调试 Eloquent 创建的查询,看看这篇文章 scotch.io/tutorials/debugging-queries-in-laravel 但是你为什么要在原始 sql 中这样做呢?
    【解决方案2】:

    找到/选择用户后,您可以通过以下方式获取相关帖子:

    $user = Auth::user(); // selecting the logged in user
    $user->posts;
    

    为此,您必须在posts 表中有user_id 列。

    如果你想拥有所有用户和他们的帖子,你可以这样做:

    $usersWithPosts = User::with('posts')->get();
    

    如果您只希望本质上至少有一个帖子的用户这样做,这将返回所有用户(无论他们是否有任何帖子):

    $usersWithPosts = User::has('posts')->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2023-01-15
      • 2016-12-30
      • 1970-01-01
      相关资源
      最近更新 更多