簡體   English   中英

Laravel查詢子查詢

[英]Laravel query subquery

例如,我有2個查詢:

1:

$q = SomeContent::select('somecontent_id')
    ->where('slug', Request::segment(2))
    ->where('something', $something)
    ->first();

2

$req = SomeContent::select('slug')
    ->where('something', $anothersomething)
    ->where('somecontent_id', $q->somecontent_id)
    ->first();

如果可能,如何在laravel的查詢生成器中將它們合並為一個查詢? 對於在where語句中使用selects語句,我找不到很多東西。

你可以聯合在一起,像

// The query builder also provides a quick way to "union" two queries together:

$q = SomeContent::select('somecontent_id')
    ->where('slug', Request::segment(2))
    ->where('something', $something);

$req = SomeContent::select('slug')
    ->where('something', $anothersomething)
    ->where('somecontent_id', $q->somecontent_id)
    ->union($q)->get();

您也可以使用orWhere函數

$q = SomeContent::select('somecontent_id')
      ->where('slug', Request::segment(2))
       ->where('something', $something)
        ->orWhere(function($query)
        {
            $query->where('something', $anothersomething)
          ->where('somecontent_id', $q->somecontent_id)
           ->union($q)->get();
        })
        ->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM