为了账号安全,请及时绑定邮箱和手机立即绑定

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条经验 获得超7个赞

你已经非常接近了,但是当你需要在括号之间有条件时,你的 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 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信