簡體   English   中英

如何使用wherehas並同時加入?

[英]How can I use wherehas and join simultaneously?

我使用Laravel 5.3

我的代碼是這樣的:

public function getWishlist($param)
{
    $num = 20;
    $q = $param['q'];
    $location = $param['location'];

    $result = $this->product_repository->whereHas('favorites', function ($query) use($q, $location) {
        $query->where('favorites.user_id', '=', auth()->user()->id)
              ->where('products.status', '=', 1);

        if($q) 
            $query->where('products.name', 'like', "%$q%");
    })
    ->join('stores', 'stores.id', '=', 'products.store_id');
    if($location)
       $result->where('stores.address', 'like', "%$location%");

    if($q) 
        $result->where('products.name', 'like', "%$q%")
              ->where('stores.address', 'like', "%$q%", 'or');

    dd($result->toSql());
    // ->paginate($num);

    return $result;
}

執行后,結果為空

似乎在哪里和加入不能同時工作

有什么解決方案可以解決我的問題嗎?

我想你可以在哪里使用高級

$result = $result->where(function($query) use ($q) {
      $query->where('products.name', 'like', "%$q%")
        ->where('stores.address', 'like', "%$q%", 'or');
})

暫無
暫無

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

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