1
votes

I'm facing some trouble at pagination on laravel. Already paginate the table. But in view page the numbers are not okay.This is how it's look like:

https://i.stack.imgur.com/407Oo.png

The other problem is when I press the numbers, links are dead and giving me this error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

This my route:

Route::get("/", "PagesController@welcome");

Route::post("/search", "PagesController@search")->name('search.route');

And this my view:

public function search(Request $request)
    {
        $q = $request->q;
        if ($q !== null && trim($q) !== ""){//here

            $estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->paginate(10);


            if(count($estates) > 0){
                return view("search", compact('estates'))->withQuery($q);
            }

        }

        $estates = array();//here
        return view("search", compact('estates'))->withMessage("No Found!");//here
    }

The other thing is in controller I can't add ->paginate(10) with ->get() it's returning as a error like:

Method Illuminate\Support\Collection::paginate does not exist.

I really appreciate any help to solve this problems. Thank you! By the way if connected or not I don't know, but I already deleted/changed "boostrap" file in public.

2
You can make a new LengthAwarePaginator. General usage like this: $new_paginate = new LengthAwarePaginator($currentItems, count($items), $perPage, $currentPage);Ali Özen

2 Answers

0
votes

You can not use paginate() method to DB facade. Because DB return collection instance. But paginate() is the method of Model. You need to manually create a paginator from a collection instance. Try this:

$estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->get();

$showPerPage = 10;

$perPagedData = $estates
            ->slice((request()->get('page') - 1) * $showPerPage, $showPerPage)
            ->all();

        $estates = new Illuminate\Pagination\LengthAwarePaginator($perPagedData, count($estates), $showPerPage, request()->get('page'));

Source: Manually Creating A Paginator

0
votes

I solved the problems.

Changed the controller right below.

$q = $request->q;
$estates = \DB::table('allestates')
                ->where("building_name","LIKE", "%" . $q . "%")
                ->orWhere("address","LIKE", "%" . $q . "%")
                ->orWhere("company_name","LIKE", "%" . $q . "%")
                ->orWhere("region","LIKE", "%" . $q . "%")
                ->orderBy('price')->paginate(10);

return view("search", compact('estates','q'));

Changed the view

@if(count($estates))
    @foreach($estates as $estate)
        <!-- ... -->
    @endforeach
    {{ $estates ->appends(['q' => $q])->links() }}
@else
    <div class="alert">
        Not found!
    </div>
@endif

I also changed the route right below.

Route::any("/search", "PagesController@search")->name('search.route');

Also added css for pagination buttons.