通过分页持久化查询数据

时间:2019-10-31 18:33:46

标签: laravel

我正在尝试按价格升序,价格降序,最新,A-Z,Z-A筛选房屋,但除第一页之后查询不存在外,我已经取得了成功。谁能告诉我一种方法吗?

public function index(Request $request)
{
    $query = $request->input('queryOptions');

    if($query == 'Price Ascending'){
        $properties = Property::orderBy('price', 'asc');
    }
    if($query == 'Price Descending'){
        $properties = Property::orderBy('price', 'desc');
    }
    if($query == 'Most Recent'){
        $properties = Property::latest();
    }
    if($query == 'A-Z'){
        $properties = Property::orderBy('city', 'asc');
    }
    if($query == 'Most Recent'){
        $properties = Property::latest();
    }

    $properties = $properties->paginate(10);
    return view('property.index', compact('properties', 'query'));
}

1 个答案:

答案 0 :(得分:2)

您可以在视图中的声明内使用appends函数-并将其用于您在控制器中声明的$ query上

{{ $properties->appends(['queryOptions' => $query ])->links() }}

https://laravel.com/docs/master/pagination