[英]Eloquent update doesn't work in laravel 5.6
I want to make update function but the function doesn't work, plese help me for resolve this problem this is my code :我想制作更新功能,但该功能不起作用,请帮我解决这个问题,这是我的代码:
This is Route这是路线
Route::resource('ItemName', 'ItemNameController');
This is Controller这是控制器
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');
}
This is View这是视图
<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>
Value when i dump this code当我转储此代码时的价值
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 => "*"
]
}
The Problem is update function is doesn't work, and i didn't get any error, can anyone help me?问题是更新功能不起作用,我没有收到任何错误,有人可以帮助我吗?
Ensure the $fillable
array is set correctly on your Eloquent model.确保在 Eloquent 模型上正确设置了$fillable
数组。
class ItemName extends Model
{
protected $fillable = [
'inc',
'item_name',
'short_name',
'definition_eng',
'definition_ind',
];
// ..
}
Normally Laravel will create a MassAssignmentException
if you call update()
and have not defined the $fillable
array.通常,如果您调用update()
并且没有定义$fillable
fillable 数组,Laravel 会创建一个MassAssignmentException
。
As soon as you define anything on the $fillable
array Laravel will silently ignore any additional values.只要你在$fillable
fillable 数组上定义了任何东西,Laravel 就会默默地忽略任何额外的值。
I encountered this problem我遇到了这个问题
My update function in the controller is as follows我在控制器中的更新功能如下
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Person $person
* @return \Illuminate\Http\Response
*/
public function update(Person $person, Request $request)
{
$person->update($request->validate([
'forename' => 'required',
'surname' => ''
]));
return redirect('/people');
}
My routes/web.php
file had the following route:我的routes/web.php
文件有以下路由:
Route::put('/people/{id}', [PersonController::class, 'update']);
To fix this, the wildcard match should be the same as the model name.要解决此问题,通配符匹配应与模型名称相同。
Route::put('/people/{person}', [PersonController::class, 'update']);
Try with fill instead of update.尝试填充而不是更新。
$itemName->fill($request->all());
$itemName->save();
Hope this will help you希望这会帮助你
你应该试试这个:
$items = ItemName::where($itemName)->update($request->all());
请注意,如果您的更新值与旧记录相同,则 update() 函数会响应 false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.