Support the ongoing development of →
Session Database Eloquent
Last updated 1 year ago.

bicatu said:

Hi, sometimes I fetch Models from the db and I'd like to make them available in further requests.

Explain, data comes from DB via a model. Can you re-phase question?



Let me try to give a simple example: I have a model with a oneToMany relationship (Product has many Detail).

Upon the first request I retrieve the Product and interact with it, for example display its properties, including the list of details.

Since I will need this information (the Product Model) on the second request I'd like to avoid having to go back and search again for the same information.

Therefore I use

Session::put('product', $product);

Where this $product is the model that I fetched and that I could interact with.

When I receive the second request I use

$product = Session::get('product');

But when I try to interact with this object I can't access the Detail(s) of it, only the properties of the main object.

So my question would be what am I doing wrong or yet what would be the best way to save a model that I fetched and still be able to access all its properties, specially for those that have relationships


Hi Anyone with ideas on this issue?


You should look into memcache or other similar solutions.

Last updated 8 years ago.

jacksoncharles said:

You should look into memcache or other similar solutions.

Hi, does it work for saving Models as well?

On a similar note sometimes I may want to persist the Model as it was at a specific time so when I come back I can use it, since the origin can be changed.

So the cache concept is not exactly what I am looking for but a safe way for persisting a Model (with its relationships) so I can access it later without having to perform the same DB lookup



Do you use eager loading when fetching a model from db? Something like:

$product = Product::with('related_things', 'other_relations')->find(1);

Sign in to participate in this thread!


Your banner here too?

bicatu bicatu Joined 22 Apr 2015


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

Your logo here?

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

© 2024 - All rights reserved.