laravel设置一个自动的where子句 [英] laravel set an automatic where clause

查看:181
本文介绍了laravel设置一个自动的where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用扩展一个generic_model的模型,而后者又是雄辩的。 (所以我有几个crud方法我使用已经设置为继承)。



很多表我使用调用软删除,这需要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;
}

reference

这篇关于laravel设置一个自动的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆