【问题标题】:add current Auth user posts to a Laravel eloquent join query将当前 Auth 用户帖子添加到 Laravel eloquent join 查询
【发布时间】:2020-05-23 00:08:12
【问题描述】:

我可以使用此方法,但不知道如何将 $user 帖子添加到此查询中:

public function getFeed()
{
    $user = $this;
    return Post::join('follows', function ($join) use ($user) {
        $join->on('posts.timeline_type', '=', 'follows.followable_type')
            ->on('posts.timeline_id', '=', 'follows.followable_id')
            ->where('follows.user_id', '=', $user->id);
    })->select('posts.*')->latest()->paginate();
}

$user 是授权用户

【问题讨论】:

    标签: mysql laravel join eloquent


    【解决方案1】:

    就我而言,我使用了Laravel Unions

    public function getFeed()
    {
        return Post::join('follows', function ($join) {
            $join->on('posts.timeline_type', 'follows.followable_type')
                ->on('posts.timeline_id', 'follows.followable_id')
                ->whereNull('follows.deleted_at')
                ->where('follows.user_id', $this->id);
        })->select('posts.*')
        ->union($this->timeline()) // $this->timeline() is query that retrieves user timeline
        ->latest()
        ->paginate();
    }
    

    【讨论】:

    • 如果你们认为有更好的性能更好的方法,请告诉我们。
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 2019-08-12
    • 2020-10-18
    • 2016-02-09
    • 2018-10-16
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多