Laravel 5 Eloquent 查询重用问题

Mah*_*Dev 1 php laravel eloquent

我正在使用 Laravel 5 雄辩查询,但遇到一些我无法弄清楚的技术问题。我想从同一个查询中提取两个单独的数组,这两个数组的条件有点不同,如下所示:

$articles = DB::table($this->table)
                ->select('articles.id','art.name');

$simpleTypes =  $articles->whereIn('articles.content_type',['Type 1','Type 2','Type 3'])->get();

$complexTypes = $articles->whereIn('articles.content_type',['Type 4','Type 5'])->get();
Run Code Online (Sandbox Code Playgroud)

获取数据$simpleTypes但进不去$complexTypes

Oli*_*dov 5

您需要克隆原始查询才能重用它:

$articles = DB::table($this->table)
                ->select('articles.id','art.name');

$clonedQuery = clone $article;

$simpleTypes =  $articles->whereIn('articles.content_type',['Type 1','Type 2','Type 3'])->get();

$complexTypes = $clonedQuery->whereIn('articles.content_type',['Type 4','Type 5'])->get();
Run Code Online (Sandbox Code Playgroud)