Eloquent 更新在 Laravel 5.6 中不起作用

Dew*_*tya 3 php laravel eloquent

我想制作更新功能,但该功能不起作用,请帮我解决这个问题,这是我的代码:

这是路线

Route::resource('ItemName', 'ItemNameController');
Run Code Online (Sandbox Code Playgroud)

这是控制器

public function update(Request $request, ItemName $itemName)
{
    request()->validate([
        'inc'            => 'required',
        'item_name'      => 'required',
        'short_name'     => 'required',
        'definition_eng' => 'max:1000', 
        'definition_ind' => 'max:1000',
    ]);

    $itemName->update($request->all());

    return redirect('ItemName')->with('success', 'Item Name Updated Successfully');
}
Run Code Online (Sandbox Code Playgroud)

这是视图

<form action="{{ action('ItemNameController@update', $ItemName->id) }}" method="post">
    @csrf
    @method('PUT')
    <input class="form-control col-4" type="text" name="inc" placeholder="INC" value="{{$ItemName->inc}}">
    <input class="form-control mt-2" type="text" name="item_name" placeholder="Item Name" value="{{$ItemName->item_name}}">
    <input class="form-control mt-2" type="text" name="short_name" placeholder="Short Name" value="{{$ItemName->short_name}}">
    <input class="form-control mt-2" type="text" name="definition_eng" placeholder="English Definition" value="{{$ItemName->definition_eng}}">
    <input class="form-control mt-2" type="text" name="definition_ind" placeholder="Indonesia Definition" value="{{$ItemName->definition_ind}}">
    <input type="submit" class="btn btn-primary" value="Edit">
</form>
Run Code Online (Sandbox Code Playgroud)

当我转储此代码时的价值

ItemName {#512 ?
#table: "tbl_item_name"
#fillable: array:5 [?
0 => "inc"
1 => "item_name"
2 => "short_name"
3 => "definition_eng"
4 => "definition_ind"
]
#connection: "sqlsrv"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:8 [?]
#original: array:8 [?]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [?
0 => "*"
]
}
Run Code Online (Sandbox Code Playgroud)

问题是更新功能不起作用,我没有收到任何错误,有人可以帮助我吗?

max*_*lms 7

确保在$fillableEloquent 模型上正确设置了数组。

class ItemName extends Model
{

    protected $fillable = [
        'inc',
        'item_name',
        'short_name',
        'definition_eng',
        'definition_ind',
    ];

    // ..

}
Run Code Online (Sandbox Code Playgroud)

通常 Laravel 会创建一个MassAssignmentException如果你调用update()并且没有定义$fillable数组。

只要你在$fillable数组上定义了任何东西,Laravel 就会默默地忽略任何额外的值。