0

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?

Hassan Raza
  • 671
  • 10
  • 27

1 Answers1

0

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

Marc
  • 5,109
  • 2
  • 32
  • 41
  • You should include relevant parts of the linked content in your answer. Always assume a link will be dead by the time it's read. – miken32 Oct 06 '20 at 18:21