在控制器laravel中查询sql

问题描述 投票:1回答:1

错误:

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以便在'&& product_price>附近使用正确的语法?'在第1行(SQL:select * from table_products where && product_price> 0)

我的代码:

public function faq(Request $request) {

    $pro = Product::all();
    $p_1  = Product::where('product_price', '>', 0,'&&', 'product_price','<', 250)->get();
    return view('fontend.errors.faq', compact('pro', 'p_1'));
}
mysql sql laravel-5.5
1个回答
1
投票

您可以在查询中添加另一个where()以应用过滤器

$p_1  = Product::where('product_price', '>', 0)
               ->where('product_price','<', 250)
               ->get();

这会产生一个类似的查询

select * from table_products where product_price > 0 and product_price < 250

或者你可以使用->whereBetween()

$p_1  = Product::whereBetween('product_price', [0, 250])
               ->get();
© www.soinside.com 2019 - 2024. All rights reserved.