简体   繁体   中英

Laravel search filter query building with pagination

I have a search form which users enter and I filter out the data then paginate. The problem is I when I paginate Laravel returns entire result

$searchFilter = arrray('id'=>true);
$building = new Building();

if (isset($searchFilter['id']))
{
    $building->where('id','=', 6);

}

return $building->paginate(20);

In the table there exist only one row with id 6. Laravel returns all the rows of the table.

However If I don't paginate it will return only the row with id 6 I want it to return paginated with applying the where operation

Figured out the answer. I should append it to $building the where operation. This is Laravel fluent works

$searchFilter = arrray('id'=>true);
$building = new Building();

if (isset($searchFilter['id']))
{
   // need to append it to building to make it work
    $building  = $building->where('id','=', 6);

}

return $building->paginate(20);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM