pasanmherath's avatar

How join two queries and do pagination

Result1 = Product::select('product_detail_id',
                                    'product_name as title')
                                ->where('category_id', 6)
                                ->where('company_id', 3)
                                ->get();

if($idArray){ $result2 = Product::select('product_detail_id', 'product_name as title') ->whereIn('category_id', $idArray) ->where('company_id', 1) ->get(); }

Need to merge $result1 and $result2 and paginate total result

0 likes
5 replies
bipin's avatar

go like this

 $resultall = $Result1 . ' ' . $Result2;
tuneless's avatar

Something like that?

    $categoryArray = [6];
    $companyArray = [3];
    if($idArray) {
      $categoryArray = $idArray;
      $companyArray = [1];
    }
    $result = Product::select(
      'product_detail_id',
      'product_name as title')
      ->whereIn('category_id', $categoryArray)
      ->whereIn('company_id', $companyArray)
      ->paginate(100);

Try to write everywhere in one query. When it's going to be more complex, I use the closure functions.

            ->orWhere(function ($query) {
                $query->where('votes', '>', 100)
                      ->where('title', '<>', 'Admin');
            })
tuneless's avatar

Hit the resolved button for the right solution :-D

Please or to participate in this conversation.