简体   繁体   English

Laravel模型覆盖子句在哪里?

[英]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. 我正在一个现有的laravel应用程序上工作,我有很多查询,所有查询都是用laravel查询生成器编写的,现在我想添加另一个where子句site_id (列)以过滤掉整个应用程序中的记录。 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 Laravel查询生成器

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 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 您可以指示特定查询排除范围,请参阅删除全局范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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