简体   繁体   中英

Laravel Model Override Where Clause?

I am working on an existing laravel application I have many queries all queries are written in laravel query builder now I want to add another where clause site_id (column) to filter out the records throughout the application. I don't want to go rewrite every query I just want a generic way to get rid off from this issue. My application queries are written in that way.

Laravel Query Builder

DB::table('users')->where('id', $user_id)->get()

Model Query Builder

AccountTag::join('ad as a', 'a.id', '=', 'abs.account_id')
         ->select('AccountTag.*', 'a.company_name')
         ->where(array("account_id" => 'xx'))
         ->get()
         ->toArray();

Can we have any overridden method that we put in the base class and add that filter to grab the required data?

How about Global Scopes or Local Scopes?

https://laravel.com/docs/5.7/eloquent#global-scopes

Look at Applying Global Scopes You can tell specific models to use a global scope.

You can instruct specific queries to exclude the scope, see Removing Global Scopes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM