Laravel: Can I somehow only hydrate models once per unique instance?
I have a many-to-many relationship, currently with about 2.5k rows in one and 100 rows in the other (think Posts/Tags). Each Post has about 3 tags. This leads to my pivot table having 7.5k rows.
When trying to do $posts = Post::with('tags')->get();debug bar tells me it hydrated 10000 models (2500 + 7500). Can I somehow change this to only hydrate 2600 (2500 + 100)? Each tag only needs to be loaded once.
I'm guessing its because of the loading of the pivot/bridging table data, but I'm in a purely display scenario, so there is no need to do syncand such.
Otherwise, I guess it's time to look into pagination on server side, was hoping to keep that on client side for now (via Datatables).
Posted Feb 29, 2020, 9:11 AM UTC
https://laravel.com/docs/6.x/eloquent-relationships#lazy-eager-loading
maybe this, it doesn’t seem to say anything about not being able to constrain it with a limit or take so maybe try that
or pagination