Php 在laravel雄辩选择上添加额外条件
我的系统中几乎每个表都有一个字段Php 在laravel雄辩选择上添加额外条件,php,laravel,eloquent,Php,Laravel,Eloquent,我的系统中几乎每个表都有一个字段id\u unity,用于控制注册表来自哪个unity 我想知道是否有可能将->get()和->lists()方法从eloquent更改为add ->where('id_unity',Session::get('unity'); 在每次选择之前,我只需执行以下操作: Products::all(); //and that actually will be Products::where('id_unity',Session::get('unity'))
id\u unity
,用于控制注册表来自哪个unity
我想知道是否有可能将->get()
和->lists()
方法从eloquent更改为add
->where('id_unity',Session::get('unity');
在每次选择之前,我只需执行以下操作:
Products::all(); //and that actually will be
Products::where('id_unity',Session::get('unity'));
有人吗?;) 您可以使用查询范围:
public function scopeGetAll($query)
{
return $query->where('id_unity', Session::get('unity'))->get();
}
然后静态调用它,不使用“scope”前缀:
如果您想让它对所有模型都可用,只需创建一个模型扩展的基本模型类,您可以在其中定义该函数
Base.php
use Illuminate\Database\Eloquent\Model;
use Session;
class Base extends Model {
public function scopeGetAll($query)
{
return $query->where('id_unity', Session::get('unity'))->get();
}
}
Products.php
use Base;
class Products extends Base {
}
请查看全局scopesPerfect!!!!工作正是我所需要的!只需等待4分钟就可以选择您的答案!非常感谢;)
use Base;
class Products extends Base {
}