V
V
Vatrush2019-05-17 16:01:40
Laravel
Vatrush, 2019-05-17 16:01:40

How to do proper Laravel pagination?

I have a template in which an Ajax view is displayed.
Ajax sends a POST request with a company id here:

Route::post('/market/getCompany', ['as' => 'getCompany', 'uses' =>'[email protected]']);

Here is the MarketController code:
public function getCompany(Request $request)
    {
        $company = Company::where('id', $request->id)->first();
        if(isset($company)){
            $articles = $this->getArticles();
            return view('layouts.market', compact('company','articles'));
        }
        return 'no results';
    }
     public function getArticles()
    {
        $articles = Articles::orderBy('price','desc')->simplePaginate(30);
        $articles->withPath('/market/articles');
        return $articles;
    }

Actually, the problem is that if I go to the next pagination page, then the result of the method crashes because the page resets Ajax and accesses the address that is intended to load the view, but if I write this in web.php:getCompany(); return 'no results';
Route::get('/market/articles', ['as' => 'getArticles', 'uses' =>'[email protected]']);

then in this case, I just get a JSON response in the browser.
Advise how to make it all qualitatively.
I changed ajax to get requests, the behavior does not change.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Yushkevich, 2019-05-19
@yushkevichv

Hello.
1) if you resolve pagination at a separate address / request, then explicitly pass the page indication in the method:
https://github.com/laravel/framework/blob/master/s...
Otherwise, it takes the default value. On the main url, you always need to send all the data both on the current page and on your company id. But keep in mind that with your design, you will always receive a view with different data. Accordingly, you will need to replace the entire html. I would do it differently (see below)
2) by design, the solution looks strange. I would separate getting the view and getting the data. Then the comment from point 1 will probably become irrelevant.
3) in the database there is a request to check the existence of exists. If you do not use this data anywhere then, then you make a too bold request in vain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question