Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel设置了一个自动WHERE子句_Php_Where_Laravel_Eloquent - Fatal编程技术网

Php Laravel设置了一个自动WHERE子句

Php Laravel设置了一个自动WHERE子句,php,where,laravel,eloquent,Php,Where,Laravel,Eloquent,我使用的模型扩展了泛型_模型,而泛型_模型又扩展了Eloquent,因此我使用的几个crud方法已经设置为继承 我使用的很多表都是invoke soft delete,它需要一个WHERE子句 WHERE deleted = 0 有没有一种方法可以使Laravel的行为方式使WHERE子句自动包含在所有查询和对彼此相关的对象的所有查询中 e、 g 然后 images where page_id = 5 and deleted = 0 在关系中,您可以在报税表中添加where_条款: pub

我使用的模型扩展了泛型_模型,而泛型_模型又扩展了Eloquent,因此我使用的几个crud方法已经设置为继承

我使用的很多表都是invoke soft delete,它需要一个WHERE子句

WHERE deleted = 0
有没有一种方法可以使Laravel的行为方式使WHERE子句自动包含在所有查询和对彼此相关的对象的所有查询中

e、 g

然后

images where page_id = 5 and deleted = 0

在关系中,您可以在报税表中添加where_条款:

 public function pages()
{
    return $this->has_many('Page')->where_deleted(0);
}
在模型中,可以添加以下内容:

public static function active()
{
    return self::where_delete(0)->get();
}
使用Page::active而不是Page::all
或者您可以从模型中的函数中删除->get,这样您还可以进一步修改查询页面::active->order_by'name'

如果您使用的是laravel 3,这就是您需要的

在你的模型上

public function query()
{

    $query = parent::query();

    $query->where('deleted','=','0');

    return $query;
}
如果您使用的是Laravel4,只需更改newQuery的方法查询


您好,newQuery不适用于Laravel4,您知道如何在Laravel4中实现这一点吗?我已经对其进行了修改,以支持Laravel4
public function query()
{

    $query = parent::query();

    $query->where('deleted','=','0');

    return $query;
}
public function newQuery()
{

    $query = parent::newQuery();

    $query->where('deleted','=','0');

    return $query;
}