如何使用会话传递的变量创建paginate

时间:2017-05-26 18:45:24

标签: php laravel session elasticsearch pagination

我使用此package在Laravel 5.4中使用弹性搜索进行研究。

我有一个屏幕供用户满足搜索要求。使用此方法,我将验证哪些字段已填充,并创建将在ES中搜索的查询。

public function search(Request $request)
{   
    $searchParams = ['vehicle', 'year_min', 'year_max', 'mileage_min', 'mileage_max', 'exchange', 'keyword', 'optionals', 'phone', 'daterange'];
    $query = ES::type("ads");

    foreach($searchParams as $param) {

        if($request->has($param)) {

            if($param == 'vehicle') {
                $results = $query->where("vehicle", "like", $request->vehicle);
            }

            if($param == 'keyword') {
                $results = $query->where("description", "like", $request->keyword);
            }

            if($param == 'year_min') {
                $results = $query->where("year", ">=", intval($request->year_min));
            }

            if($param == 'year_max') {
                $results = $query->where("year", "<=", intval($request->year_max));
            }

            if($param == 'mileage_min') {
                $results = $query->where("mileage", ">=", intval($request->mileage_min));
            }

            if($param == 'mileage_max') {
                $results = $query->where("mileage", "<=", intval($request->mileage_max));
            }

            if($param == 'exchange') {
                $results = $query->where("exchange", "like", $request->exchange);
            }

            if($param == 'optionals') {
                $results = $query->where("optionals", "like", serialize($request->optionals));
            }

            if($param == 'phone') {
                $results = $query->where("phone", "exists", true);
            }

        }
    }

    $all_leads = $results->orderBy("created_at", "desc")->paginate(15);
    $all_leads->setPath('/dashboard/crawler/resultados');

    Session::put('all_leads', $all_leads);
    return redirect()->action('DashboardAdmin\ProspectingLeadController@results');
}

挂载查询后,我调用另一种方法来呈现结果并按会话传递数组。

$all_leads = $results->orderBy("created_at", "desc")->paginate(15);
$all_leads->setPath('/dashboard/crawler/resultados');

Session::put('all_leads', $all_leads);
return redirect()->action('DashboardAdmin\ProspectingLeadController@results');

当我通过会话搜索结果并发送到视图时,paginate不起作用只列出第一页的结果,如果我单击分页按钮,它会更改URL但不会更改记录。

public function results()
{
    $all_leads = Session::get('all_leads');
   return view('dashboard_admin.prospecting_lead.index', compact('all_leads'));
}

1 个答案:

答案 0 :(得分:2)

在结果方法中,您将从缓存中获取结果,因此分页器不起作用。它每次只会获得15个结果。要使paginator工作,您需要调用paginator,以便它可以根据查询字符串获取结果。