laravel 如何在laravel中根据多个where条件作为数组删除记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28689435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:00:37  来源:igfitidea点击:

How to delete records based on the multiple where conditions as an array in laravel

phparrayslaraveldelete-record

提问by Kiran

I am searching in docs and in the stack exchange for days that, is there any way that I can actually pass an array with multiple conditions in it to delete the record in the Laravel 4.2?

我在文档和堆栈交换中搜索了几天,有什么方法可以实际传递一个包含多个条件的数组来删除 Laravel 4.2 中的记录?

Example

例子

I want to achieve something like below

我想实现以下目标

DELETE FROM `employees` WHERE user_id = 5 AND dept_id = 5

For this can I do something like below?

为此,我可以执行以下操作吗?

$whereArray = array('user_id'=>5,'dept_id'=>5);

return DB::table('employees')->where($whereArray)->delete();

I know I can use multiple where conditions to achieve this. But for every time a new condition arrives I have to rewrite the function. And also I cannot use this function as dynamic one.

我知道我可以使用多个 where 条件来实现这一点。但是每次出现新条件时,我都必须重写该函数。而且我不能将此功能用作动态功能。

So please help me this? How can achieve this using the array?

所以请帮我这个?如何使用数组实现这一点?

回答by patricus

You can't directly pass in the array, but you could process the array:

不能直接传入数组,但可以处理数组:

$whereArray = array('user_id' => 5,'dept_id' => 5);

$query = DB::table('employees');
foreach($whereArray as $field => $value) {
    $query->where($field, $value);
}
return $query->delete();

This functionality can be extracted out into a function, or even a model scope, that accepts your array and builds and returns the query to you.

此功能可以提取到一个函数中,甚至是一个模型范围,它接受您的数组并构建并向您返回查询。

For example, if you have an Employeemodel:

例如,如果您有一个Employee模型:

class Employee extends Eloquent {

    public function scopeWhereArray($query, $array) {
        foreach($array as $field => $value) {
            $query->where($field, $value);
        }
        return $query;
    }

}

Then you could do:

那么你可以这样做:

$whereArray = array('user_id' => 5,'dept_id' => 5);

return Employee::whereArray($whereArray)->delete();

Edit

编辑

If you wanted to be able to supply the operator, as well, you'd just need to change the format of your array:

如果您还希望能够提供运算符,您只需要更改数组的格式:

$whereArray = array(
    array(
        'field' => 'user_id',
        'operator' => '=',
        'value' => 5
    ),
    array(
        'field' => 'dept_id',
        'operator' => '=',
        'value' => 5
    ),
    array(
        'field' => 'salary',
        'operator' => '<',
        'value' => 5000
    )
);

return Employee::whereArray($whereArray)->delete();

And you would need to update your function:

你需要更新你的函数:

class Employee extends Eloquent {

    public function scopeWhereArray($query, $array) {
        foreach($array as $where) {
            $query->where($where['field'], $where['operator'], $where['value']);
        }
        return $query;
    }

}