mysql - Add condition to all requests in Laravel -


i'm creating online tool companies each have set of users in laravel.

when user connected, has $connected_company_id variable

  • for every select request (called ::all(), find(), ...), add condition: where company_id = $connected_company_id. have found post: laravel set automatic clause, doesn't work overriding newquery().

  • for every insert request, add company_id.

is possible without changing code inside controllers ?

i thought extending eloquent customeloquent, , make models extend customeloquent, don't know how write code customeloquent , if work.

well, make use of eloquent model events. assume have connected_company_id stored in session company_id

class basemodel extends eloquent{     public static function boot(){          parent::boot();          //column inject when inserting          static::creating(function ($obj){              $obj->company_id = session::get('company_id');          });           //column inject when updating          static::updating(function ($obj){              $obj->company_id = session::get('company_id');          });    } } 

you can extend basemodel class on models want company_id inserted or updated. take @ eloquent model events more information.

the above code automatically insert or update company_id model extend basemodel to. when model::all() or model::get(), automatically company_id on model , can perform searches requested on point `

hope helps.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -