亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 從 RAW 查詢到 Eloquent 查詢

Laravel 從 RAW 查詢到 Eloquent 查詢

PHP
搖曳的薔薇 2023-09-08 14:27:54
我正在嘗試將我的轉換Raw query為eloquent query你可以在這里看到 $data = DB::connection('mysql')->select("SELECT product.id, product.title, product.description, product.content, product.photo, product_per_category.productcategory_id, product.quantity, product.price         FROM product LEFT JOIN product_per_category ON product_per_category.product_id = product.id        WHERE product.deleted = 0 AND product_per_category.deleted = 0 AND productcategory_id = '$id' AND (product.title like '%$keyword%' OR product.content like '%$keyword%' OR product.price like '%$keyword%' OR product.quantity like '%$keyword%')                GROUP BY product.id");我有多個語句與括號內的&WHERE結合ANDOR我只是想知道我這樣雄辯的查詢是否正確地做到了這一點 $data = DB::table('product')    ->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')    ->join('product_per_category','product_per_category.product_id','=','product.id')    ->where(['product.deleted' => 0])    ->where(['product_per_category.deleted' => 0])    ->where(['product_per_category.productcategory_id' => $id])    ->orWhere('product.content', 'like', '%' . $keyword . '%')    ->orWhere('product.title', 'like', '%' . $keyword . '%')    ->orWhere('product.quantity', 'like', '%' . $keyword . '%')    ->orWhere('product.price', 'like', '%' . $keyword . '%')    ->groupBy('product.id')    ->get();因為我想知道在我的查詢中我的OR括號內有語句。我將它們組合在括號內,使其僅是LIKE聲明中可選的字段
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

你已經非常接近了,但是當你需要在括號之間有條件時,你的 where() 函數應該是一個回調。


例如(product.title like '%$keyword%' OR product.content like '%$keyword%' OR product.price like '%$keyword%' OR product.quantity like '%$keyword%') 將是


$query->where(function($subquery) use($keyword) {

    $subquery->where('title', 'like', "%{$keyword}%")

    ->orWhere('content', 'like', "%{$keyword}%");

});

這只是您的要求的一個粗略示例,但您應該得到它。


正如您所知,您可以組合幾乎所有 Eloquent 功能。


祝你好運!


查看完整回答
反對 回復 2023-09-08
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您可以在方法內部傳遞一個閉包where。閉包將接收一個查詢生成器實例,您可以使用它來設置應包含在括號內的約束。

這被稱為parameter grouping

https://laravel.com/docs/7.x/queries#parameter-grouping

將您的聲明更改為此。

$data = DB::table('product')

    ->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')

    ->join('product_per_category','product_per_category.product_id','=','product.id')

    ->where(['product.deleted' => 0])

    ->where(['product_per_category.deleted' => 0])

    ->where(['product_per_category.productcategory_id' => $id])

    ->where(function($query) use($keyword){

        $query->where('product.content', 'like', '%' . $keyword . '%')

              ->orWhere('product.title', 'like', '%' . $keyword . '%')

              ->orWhere('product.quantity', 'like', '%' . $keyword . '%')

              ->orWhere('product.price', 'like', '%' . $keyword . '%');

    })

    ->groupBy('product.id')

    ->get();


查看完整回答
反對 回復 2023-09-08
  • 2 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號