laravel设置一个自动的where子句 [英] laravel set an automatic where clause
问题描述
很多表我使用调用软删除,这需要where where where where删除= 0。
有没有办法让laravel的行为方式使得这个where子句被自动包含在所有查询中,并且所有查询都与相关的对象
例如
页面,其中id = 5并删除= 0
然后
图像,其中page_id = 5并被删除= 0
如果你使用laravel 3这是你需要的
public function query()
{
$ query = parent :: query();
$ query-> where('deleted','=','0');
return $ query;
}
如果您使用laravel 4只需更改方法查询 newQuery
public function newQuery()
{
$ query =父:: newQuery();
$ query-> where('deleted','=','0');
return $ query;
}
I use models that extend a generic_model, which in turn extends eloquent. (so that I have several crud methods I use already set to be inherited).
A lot of the tables I use invoke soft delete, which needs a where clause of where deleted = 0.
Is there a way to make laravel behave in such a way that this where clause is automatically included in all queries and all queries to objects that are related to one another.
e.g.
pages where id = 5 and deleted = 0 and then images where page_id = 5 and deleted = 0
if you're using laravel 3 this is what you needs
on your model
public function query()
{
$query = parent::query();
$query->where('deleted','=','0');
return $query;
}
if you're using laravel 4 just change the method query for newQuery
public function newQuery()
{
$query = parent::newQuery();
$query->where('deleted','=','0');
return $query;
}
这篇关于laravel设置一个自动的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!