Laravel 8 -搜索页(withQueryString)不工作时,搜索行整数上的数字表



当涉及到搜索row int (price)上的数字时,我不能搜索,但当搜索row string (name)上的字符串数字时,它工作得很好

在我的表行中像这样的这里是我的Product表

category_id
"56"
user_id
"1"
name
"555"
description
"fruit"
price
555

当我搜索name行"555"它工作得很好,因为它是string。但是当涉及到价格时,我无法搜索,因为它是int

这里是我的代码控制器搜索
public function index(Request $request){
$user = Auth::user()->id;
if($request->search){
$search = $request->search;
$products = Products::with('category')
->where('name','like',"%$search%")
->orWhere('price','like',"%$search%")
->where('user_id',$user)->paginate(10);
}else{
$products = Products::with('category')->where('user_id',$user)->paginate(10);
}
return view('client.product.index',['products'=>$products]);
}

这是我的刀

<form class="w-full" action="{{ route('client.product.index') }}">
<i class="fa fa-search"></i>
<input placeholder="Search"  type="search" name="search">     
<button type="submit">Search</button>
</form>
@foreach ($products as $product)
<p>{{ $product->name }}</p>
<p>{{ $product->price }}</p>
@endforeach
{{ $products->withQueryString()->links('pagination::bootstrap-4') }}

您需要对where和orWhere进行分组,以便正确过滤user_id:

$products = Products::with('category')
->where( function($q) use ($search) {
$q->where('name','like',"%$search%");
$q->orWhere('price','like',"%$search%");
})  
->where('user_id',$user)->paginate(10);

最新更新