iftikharuddin's avatar

How to Paginate the array?

I am working on a webapplication, which searches for places/locations via /search route. On /search route it displays all the matched/searched locations BUT the locations are array based i want to paginate through it which i did but now the problem arises in the view. In the view i used {!! $father->render() !!} which renders the pagination but when i try to go to the 2nd it doesn't work.

Controller Code:

{
    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'));
            }
            
        }
        

    }
} ```
0 likes
14 replies
iftikharuddin's avatar

@themsaid here is the error "LogicException in RouteCompiler.php line 102: Route pattern "/catsearch/{id}/{{id}}" cannot reference variable name "id" more than once."

I'm confused why it shows this error, the route in which pagination is not working is /search.

iftikharuddin's avatar

Image: http://oi64.tinypic.com/23t2vdg.jpg

Routes are :

for this search route pagination is not working when i click on 2nd or any other page.

Route::post('/search', 'AdSearchController@postSearch');
Route::resource('/ads', 'AdSearchController');
//Route for category wise search, pagination works perfect in here
Route::resource('/catsearch/{id}', 'AdSearchController@postCatSearch');```
richard's avatar

Use post method instead of resource here since you are sending a POST request to a single method.

Route::post('/catsearch/{id}', 'AdSearchController@postCatSearch');
1 like
bobbybouwmann's avatar

You can replace Paginator::resolveCurrentPage(), with $page, since it's the same value. No fancy Facades needed here

1 like
iftikharuddin's avatar

@richard it throws this error when i change route to post MethodNotAllowedHttpException in RouteCollection.php line 218:

bobbybouwmann's avatar

@richard If you say that it needs to be a post request because of the injected Request than you are wrong. You can inject it as well on get requests. It should be a get request here since the url will look like this

/search?page=2
1 like
iftikharuddin's avatar

yes the URL looks exactly like that @bobbybouwmann but it throws (Route pattern "/catsearch/{id}/{{id}}" cannot reference variable name "id" more than once. ) error.

as i mentioned earlier i have two routes one is category wise search which is via /catsearch route and in there pagination works perfect. The problem is with the /search route where pagination is not working and throwing the above error.

bobbybouwmann's avatar

You can't have two url parameters with the same name. You should use different names here.

We are going passed the purpose of your code here. What are you exactly trying to achieve and what is not working for you? Keep it simple first! So only paginate a simple array and from there extend to your code

1 like
iftikharuddin's avatar

@bobbybouwmann Actually i have an array named $father, i have paginated that successfully but in the view when i click on the 2( which is going to open URL search?page=2) it throws this error(Route pattern "/catsearch/{id}/{{id}}" cannot reference variable name "id" more than once). And i don't have two url parameters with same name, you can see my routes here

Routes:

Route::resource('/ads', 'AdSearchController');

Route::resource('/users', 'UsersController');
Route::resource('/catsearch/{id}', 'AdSearchController@postCatSearch');```
1 like
iftikharuddin's avatar
iftikharuddin
OP
Best Answer
Level 1

All i did was to change the POST request for search to GET in the Route and also in the form view. Actually we don't need to POST data in search, we can simply use GET request for search data from database.

Route::get('/search', 'AdSearchController@postSearch');
bobbybouwmann's avatar

Seriously! That's what I told you before!

It should be a get request

1 like

Please or to participate in this conversation.