Laravel 更新不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44404651/
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 16:05:47  来源:igfitidea点击:

Laravel Update not working

phplaravel

提问by Saurabh Parekh

I have table called admins and model called Admin.

我有名为 admins 的表和名为 Admin 的模型。

When i try to update the records it fails every time with one field.

当我尝试更新记录时,它每次都用一个字段失败。

$admindetail = Admin::find($id);
$admindetail->fill(['is_delete'=>1])->save();

Above code is not updating record in DB. datatype for is_deleteis tinyint.

上面的代码没有更新数据库中的记录。数据类型为is_deletetinyint。

If i update name field it works with the same code like

如果我更新名称字段,它可以使用相同的代码

$admindetail = Admin::find($id);
$admindetail->fill(['name'=>'abc'])->save();

Can you help me why first code is not working.

你能帮我为什么第一个代码不起作用。

回答by Ramzan Mahmood

Modal use to interact with table so all attributes which you need to store in DB you should need to define in your model as your Admin modal should look like

模态用于与表交互,因此您需要存储在数据库中的所有属性都需要在模型中定义,因为您的管理模态应该看起来像

   <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    protected $fillable = ['name', 'is_delete'];
}

You missing is_deletefrom your Modal code.

你错过is_delete了你的模态代码。

回答by sathish R

Instead of passing value as array, you could try the code below:

您可以尝试以下代码,而不是将值作为数组传递:

$admindetail = Admin::find($id);
if(!$admindetail)
{
  $admindetail->is_delete=1;
   $admindetail->->save();
}

回答by Andrey Lutskevich

Use boolean type, no integer 1: $adminDetail->fill(['is_delete' => true])->save();

使用布尔类型,无整数 1: $adminDetail->fill(['is_delete' => true])->save();

And you can use as property without fillmethod and you do not need accept is_deletein the $fillablearray:

您可以在没有fill方法的情况下用作属性,并且不需要is_delete$fillable数组中接受:

$adminDetail->is_delete = true;
$adminDetail->save();

回答by Gilbert Tuazon

You should check your fillable in your model like this:

您应该像这样检查模型中的可填充物:

protected $fillable = [
    'name',
    'is_delete'
];