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

eddstep's avatar

Query Builder -> middleware -> pagination

Hi, I don't understand how to work with data getting from query Builder to modify it. Here is something like: I get data and send to blade with pagination

$invoices = $this
            ->startConditions()
            ->select($columns)
            ->orderBy('invoices.id', 'desc');
return view('livewire.invoice.list', [
            'invoices'      =>  $this->invoices->paginate(10)
        ]);

but I need before sending to view, make something with data, something like middleware. How it works? Of course after that

   'invoices'      =>  $this->invoices->paginate(10)

must stay working

0 likes
11 replies
Sinnbeck's avatar

And what is it that you need to do before returning the view?

eddstep's avatar

@Sinnbeck ))) great question, thanks.

For example, I need different messages in view for each invoice, if it includes null in column or data is more than "limit". And I want to include these messages to object before sending.

Is it enough?

Sinnbeck's avatar

@eddstep so on each row you need to show a message? Or one message for the page if any of those 10 items has a null value?

eddstep's avatar

@Sinnbeck on each row its own message according result of custom function. Other words I need new column in list with message depending data. Of course, its possible later, for each invoice get this message requesting from view, but I guess it's better to form this messages first.

eddstep's avatar

@Sinnbeck As I understand mutators not for this. I don't need to customize data before saving to db. I get my object , and I send it to view with paginate. This object ($invoices) I show as list, but data in object, that I got from db is not enough. I need also to fill items in object additional data , that is not from table column, it's just generated according data for each item and I have to get it in foreach loop. PS about 'through()" may be it's better idea.

eddstep's avatar

@Sinnbeck thanks, but as I see it's not easy way, because paginate() just does a chunk request and it's available for my question only after request rebuild paginate->items()

Sinnbeck's avatar

@eddstep that's why you can use through(). That changes items but not the paginator

Snapey's avatar

use an accessor, its the simplest solution

eddstep's avatar

thanks, yes accessors that's what I need, initially was not clear for me. Thanks

Please or to participate in this conversation.