Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

princeoo7's avatar

Setting a Global Pagination Limit Variable in laravel

This is kinda stupid question, but anyway clearing out the doubt is better then keeping it to self.

so currently i have set a varaible in config/app.php

'limit' => 20

then in any controller i need it, i do

Config('app.limit')

now in any controller i am doing it as

protected $limit;

    public function __construct(Request $request)
    {
        ($request->has('limit')) ? $this->limit = $request->input('limit') : $this->limit = Config('app.limit');
    }

is this the right way ?

in future what if i have to give user the ability to set and save limit value for search in future too.

should i make a setting tables in db and have a column with name limit and store the value against the user id ?

if so what will be the best way to access this value when logged in in real life production application ?

Session or cookie or something else ?

if it all comes storing such data to db, then what all normally do people store in settings table ? for example for eCommerce or academy or accounts related projects.

I am asking about general things which we can save it in db.

0 likes
8 replies
Cronix's avatar
Cronix
Best Answer
Level 67

For your first code, I think this is cleaner.

$this->limit = $request->input('limit', config('app.limit'));

You can pass a default value as the 2nd parameter if it doesn't exist in request. https://laravel.com/docs/5.7/requests#retrieving-input

For your other question, yes you could create a settings table, or just put it as a field on the users table. It depends on what you're doing. My app allows the user to set their timezone, and then all dates returned get converted to their timezone for display in their local timezone. It's basically the same thing. Just create a helper function or something, that would get the limit, depending on how your app needs it.

For example, what if you allowed both options? Allowed them to set it on their user settings, and also allowed them to change it via a dropdown (which I assume is where $request->limit comes from). You'd need some logic to determine which one to use, which would be in the helper function. Do you use the default, the one in the user settings, or the one in the dropdown?

1 like
princeoo7's avatar

@CRONIX - One or two more things which i want to know and as they might be small as this one, i hope if i am extending this in this thread itself. hope you can help.

public function index(Request $request)
    {
    //get all articles
    $records = Article::orderBy('id', 'DESC')->paginate($this->limit);
    $pageTitle = 'Articles Dashboard';
    $totalCount = Article::count();

    return view('backend.article.dashboard', compact('pageTitle', 'totalCount', 'records'));
    }

i am calling to URL/admin/blog/dashboard to get all listing of article.

now as you can see i have implemented pagination here.

when i came to the blade part, i realized i need to implement a search too for the same page to make users life ease.

  1. now here i want to use a view component. how what should i do ? make a axios call from the component to an api ? or pass the data via props which i got from the page load time.

  2. In article controller i already have the defaults index, create, store, update, edit, destory functions. if i got the api way, is it ok and good practice to make that in the same controller ?

public function getAllArticles()
{
    $records = Article::orderBy('id', 'DESC')->paginate($this->limit);
    return compact('records');
}

something like this and implement a search here ?

princeoo7's avatar

@CRONIX - i replaced my code with your suggested code but i am getting the below error

Function name must be a string

protected $limit;

    public function __construct(Request $request)
    {

        $this->limit = $request('limit', config('app.limit'));
}

Cronix's avatar

Sorry, should have been $request->input('limit', config('app.limit')). I'd suggest making a helper function or a trait and use it on the classes that need pagination, not putting it in the controllers construct.

princeoo7's avatar

@CRONIX - no problem :)

anything on the two questions i asked in my later comment after your answer ?

Cronix's avatar

It's generally best to keep topics in a thread to a single subject. That helps other finding solutions to similar problems without a bunch of unrelated stuff to wade through. People can then search for threads that have a marked solution and quickly find the answer they are looking for.

The 2 questions are really specific to your own app. I don't have an answer for how you should architect it. I don't know the data or requirements or how everything is built so far.

As far as an api, that depends on the architecture of your app. Would I build an api just to do this? No. I'd just create an additional web route that just returns json or something for the search results, but it would be a different controller for handling it (search functionality).

1 like

Please or to participate in this conversation.