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

MaciejSuchanski's avatar

how to paginate db::select

$sql = DB::select("select * from web_v2.web_mnp('','','','','','','','','','','')") ->paginate(10);

    return Inertia::render('Mnp/View', [
        'sql' => $sql
    ]);

This gives error 'Call to a member function paginate() on array'. I need to use postgresql functions instead of tables, sql is to complicated to rewrite in eloquent and i do not want it in there. I also want to use laravel paginate() as it works very well with element plus el-paginate and el-table. Please don't suggest changing to eloquent this is how i want it.

0 likes
6 replies
tykus's avatar

You cannot since select will execute the query already. You could possibly use

DB::table('web_v2.web_mnp')->paginate()

however, I don't understand web_v2.web_mnp('','','','','','','','','','',''); what does it mean?

MaciejSuchanski's avatar

@tykus its postgresql function values you put in '' will be available inside the function as for example $1.

DB::table('web_v2.web_mnp')->paginate();

this won't work, it gives not found. So laravel have no support whatsoever for custom functions? Just tables?

martinbean's avatar

@MaciejSuchanski You‘re asking about two completely different things.

You’re executing a basic SELECT statement. You don’t “paginate” a SELECT statement. Pagination is the action of adding OFFSET and LIMIT clauses to your queries.

MaciejSuchanski's avatar

@martinbean i know i do use limit and offset too but i would like to use laravel paginate() this select returns data the same way select * from users do. It's just in a function.

tykus's avatar

@MaciejSuchanski understood, my error was assuming web_mnp is a table 🤦‍♂️

Next question I would have... is the result of the custom function even paginatable; could you write LIMIT / OFFSET into such a query in postgres?

MaciejSuchanski's avatar

@tykus yes, it's just a normal table when you run it, that's why i'm so confused why i can't paginate it. I want it in this form so i will have the same code that i run in local sql editor and my coworkers see on website. That's why i don't like eloquent. This function have 8 joins and around 300 lines. I have an old version of the website with it using limit and offset and dedicated count function to get total rows. In all honesty i already gave up i will just add it back i wanted to reduce amount of code, but i guess it's impossible right now. :/

Please or to participate in this conversation.