Support the ongoing development of Laravel.io →
Database Eloquent

Hi! I am creating a second project for a company.

The first one was written by me in Yii framework. Now I want to switch into Laravel.

In Yii, in model, I have beforeFind and beforeSave functions in ActiveRecord. I can't see it in Laravel. Why I need this? I need to create application with different shops. Shops can be added to users. User "user1" has 3 shops: shop1, shop2, shop3. In menu I want to have Change shop and then user can switch the shop and all showed, added, deleted data should belongs to that shop. User can't see anything from shop 4. I want to automate this to not remember every time to add condition shop_id = XXX.

In beforeSave I want to add shop_id to every object.

Also I need some models to be global for all shops (ie. global settings, users).

How can I achieve this in Laravel?

Thank's for all advices

Last updated 3 years ago.
0

A session wouldn't work somehow?
Could you dig into yii and see how they accomplished it?

Last updated 10 years ago.
0

I save in session while loging user shop_id, and when switching shop I save shop_id in session too. In Yii there is function BeforeSave and BeforeFind in ActiveRecord and I create my own MyActiveRecord : ActiveRecord with beforeSave and BeforeFind method. In this method I add compare to CdbCriteria ($crtiteria->compare('shop_id', $session['shop_id']) and then call parent find.

I need someting like this in Laravel :)

0

When you say menu, you do mean a menu in the view. If that is the case, could the user shops (other pages) be recorded in the database, then in menu use a dropdown to select a different shop, make links in the dropdown. Or you could have tabbed links at top or bottom of page. Are the shops other pages, or exactly what?

0

I need to call Product:all() and get only products connected with specific shop (saved in session). Products has shop_id attribute.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

kpyrdol kpyrdol Joined 11 Dec 2014

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.

© 2025 Laravel.io - All rights reserved.