Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

This is how I see it.
A relation represents only the possibility to make a query to the database. I is a query builder and that is what those public functions like 'products' return.
When calling get(), first(), count() or detach()... the query is send to the DB and the eventual result is stored in your variable ($products).
That variable doesn't know what is happening in the program and it doesn't remove any of its items if you make a query to the datase to remove some enties. It's up to you know what is the state of your database and what is temporary stored in your variables.
Those collections are not linked with the database, that are just variables, arrays, values, 0's and 1's.

Last updated 1 year ago.
0

Hi Firtzberg,

Well, I do get the principle that these objects are disconnected from the DB, but I am kind of expecting that if I call detach() on an object, that it will also be removed from the in-memory collection without having to go back to the DB and query again to refresh.

In some other ORMs, calling detach(), or its equivalent, would not immediately update the DB until a call to update() or save(), but it would immediately remove the object from the in-memory collection (or at least flag it as removed).

In Eloquent's case, it removes the relation from the pivot table immediately, but it leaves the object "dangling" in memory.

I see that as some kind of optimization that could be added, maybe as an optional parameter to the detach() method ?

I don't know if that would be a good thing or not, but at least now I know that it's my responsibility to track such cases.

Thank you for the clarification.

Cheers

Last updated 1 year ago.
0

Hi there, I'm experiencing the same situation about "detaching", did you find any answer around this issue?

thank you in advance

0

Hi again,

I have found an answer to this in stackoverflow, just in case it serves to anybody looking for an answer.

$model->load('relation');

Link to source

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.