I have an attribute which is somewhat intensive to run. I want to append the attribute to the model only when needed, so I'd like to use the setAppends() method when I need it, rather than the $appends property which would append the attribute every time.
The context I'm doing it in is confusing me. The model with the attribute is being eager loaded on a relationship using the with() method and the whole lot is being paginated. Here's the code:
$appointments = Appointment::with([
'customer',
'customer.lead.source',
'rep',
'status',
'quote',
'quote.basket',
])->paginate();
The Quote model has an attribute called pricePerSquareMetre, so I'd like to use this:
->setAppends('price_per_square_metre')
I can't figure out where to put it. In the past when I've had a collection, I've iterated through the items in the collection using each() and used setAppends() in there, but I'm not sure how to iterate over the items in the paginated results. Also, I'm sure there's a better way.
I tried this, but the parameter passed to the closure is the relation method, which doesn't support setAppends().
'quote' => function ($q) {
$q->setAppends('price_per_square_metre');
},
Any iteas?