如何在Laravel 5.3中对阵列进行分页?

时间:2016-11-30 10:36:23

标签: laravel laravel-routing

我正在开发一个网络应用程序,通过 /搜索路径搜索地点/位置。在/搜索路线上它显示所有匹配/搜索位置但是位置是基于数组我希望通过它进行分页,但是现在问题出现在视图中。在我使用的视图中

  

{!! $ father-> render()!!}

呈现分页但是当我尝试去第二时它不起作用。

控制器代码

{
public function paginate($items,$perPage)
{
    $pageStart = \Request::get('page', 1);
    // Start displaying items from this number;
    $offSet = ($pageStart * $perPage) - $perPage; 

    // Get only the items you need using array_slice
    $itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);

    return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
/**
 * When user submit the search form it will return all the locations.
 *
 * @return locations collection
 */
public function postSearch(Request $request)
{

    $categoryid = $request->category_id;
    $locationid = $request->location_id;
    $validator = \Validator::make($request->all(), [
        'category_id' => 'required',
        'location_id' => 'required',

    ],
    [
        'category_id.required'  => 'Select Category',
        'location_id.required'  => 'Select Location',

    ]);

    if($validator->fails()) {
        return back()->withInput()->withErrors($validator->errors());
    }else{ 
        $category = Category::where('id', $categoryid)->first();
        if(!$category->locations->isEmpty()) {
            $cities = Location::where('id', $locationid)->pluck('city');
            $cityname = $cities[0];

            $alllocations = [];
            $ratecards = [];

            $father = [];

            $i = 0;
            $filteredlocations = $category->locations->where('city', $cityname)->all();
            foreach($filteredlocations as $location){

                $alllocations[$i] = $location->getaddetails;
                $ratecards[$i] = $location->ratecard;
                $father[$i]['ad'] = $alllocations[$i];
                $father[$i]['rate'] = $ratecards[$i];
                $i++;
            }

            $perPage = 4;

            $father = AdSearchController::paginate($father, $perPage);

            $nothing = 1;
            return view('ads', compact('father','nothing'));
        }else {
            //flag
            $nothing = 0;
            return view('ads', compact('nothing'));
        }

    }


}

}

Route :: resource('/ search','AdSearchController @ postSearch');这是路线。我们正在寻找这条路线。

0 个答案:

没有答案