如何更新多个记录并在控制器中使用

时间:2019-02-01 20:17:14

标签: php laravel

我想更新记录,但是如果字段为零,则将其转换为1,反之亦然,该字段是is_published

$Product=Product::whereIn('category_id', [$id])->update(['is_published'=>0]);

2 个答案:

答案 0 :(得分:0)

您可以对特定字段使用原始sql进行此类操作:

int()

由于update方法返回受影响的行数,因此您应查询Product模型以获取更新的行:

Product::whereIn('category_id', [$id])->update([
    'is_published' => DB::raw('!is_published')
]);

答案 1 :(得分:-1)

假设零字段是“ is_published”,则具有类似的内容

$p_id = Product::find($id);
$pd = 0;
if ($p_id->is_published ==0) {
    $pd=1;
}
$product = Product::whereIn('category_id', [$id])->where('is_published', $pd)->update(['is_published' => 0]);

尚未测试代码