Php 如何将以下三个Laravel雄辩的查询合并为一个?

Php 如何将以下三个Laravel雄辩的查询合并为一个?,php,mysql,laravel,eloquent,laravel-5.6,Php,Mysql,Laravel,Eloquent,Laravel 5.6,向Laravel和mysql专家快速提问,我有以下三个问题:- // Only Can use slug here $post = Post::whereSlug($slug)->with(['category', 'author'])->first(); $prevPost = Post::where('id', '<', $post->id) ->select('id', 'title', 'slug') -&

向Laravel和mysql专家快速提问,我有以下三个问题:-

// Only Can use slug here
$post = Post::whereSlug($slug)->with(['category', 'author'])->first();


$prevPost = Post::where('id', '<', $post->id)
            ->select('id', 'title', 'slug')
            ->orderBy('id', 'desc')
            ->first()

$nextPost = Post::where('id', '>', $postId)
            ->select('id', 'title', 'slug')
            ->orderBy('id')
            ->first()
//这里只能使用slug
$post=post::whereSlug($slug)->with(['category','author'])->first();
$prevPost=Post::其中('id','',$postId)
->选择('id','title','slug')
->orderBy('id')
->第一()

有没有办法把这三个查询合并成一个查询。我希望将所有三个查询合并为一个查询,对于最后两个查询,我需要$post->id。我希望有一个包含所需帖子的结果,前一篇帖子到所需帖子,下一篇帖子到所需帖子

最好的方法是使用某种范围来实现:

public function scopeOfId($query, $id, $operator, $orderBy = "asc") {
   $query->where('id', $operator, $id)
        ->select('id', 'title', 'slug')
        ->orderBy('id', $orderBy)
}

然后您可以将其称为
Post::ofId(5,"将所有这些查询合并到一个查询中并没有实际有效的方法。您可以使用流畅的原始查询和一些复杂的窗口函数,但是编写这些查询会很耗时,并且取决于您的特定SQL类型。

您可以使用
或where
将所有查询合并到一个查询中,或者where不起作用p我想,因为我想将所有三个查询合并为一个查询,对于最后两个查询,我需要$post->id。我想通过查询生成器和嵌套选择,得到一个包含所需帖子、上一篇文章到所需帖子和下一篇文章到所需帖子的结果。我认为这是可能的,但问题是如何实现的?谢谢您的回复,但这是可能的我要求的不是应付。