Home > Net >  Laravel 8 - search paginate ( withQueryString ) not working on table when searching NUMBERS on row i
Laravel 8 - search paginate ( withQueryString ) not working on table when searching NUMBERS on row i

Time:11-29

i can't search when it comes to searching NUMBERS on row int (price), but when searching like String number on row string (name) it's working fine

like this in my table row here is my Product table

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

when i am searching the name row "555" it's working fine beacuse its a string. but when it comes to price i can't search it because it's int

here is my code for controller searching

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]);
}

here is my blade

    <form  action="{{ route('client.product.index') }}">
        <i ></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') }}

CodePudding user response:

You need to group the where and orWhere so the user_id will be correctly filtered:

 $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);
  • Related