【发布时间】:2018-06-13 05:00:02
【问题描述】:
我正在调用这个控制器来更新模型:
public function update(Request $request, $id)
{
$question = Question::find($id);
$this->authorize('edit', $question); // uses Laravel's built-in Policy framework
$updateArray = [
'question' => $request->question,
'type_based_id' => $request->type_based_id,
];
//$question = Question::where('id', $id);
$question = $question->update($updateArray);
// Add the id into the array for return to the client
$updateArray["id"] = $id;
return ['message' => 'Question Updated', 'data' => $updateArray];
}
上面的代码在调用$question->update() 时会引发MassAssignmentException。如果我取消注释 $question = Question::where('id', $id); 它会起作用。
我做了一些记录,似乎 find() 返回了我的模型的一个实例 (App\Question),而 where() 返回了一个构建器 (Illuminate\Database\Eloquent\Builder)
如何在不发出两个单独的数据库请求的情况下同时满足 authorize() 和 update() 的要求?
谢谢!
【问题讨论】:
-
不太清楚你在问什么。授权与您的更新调用无关。 MassAssignmentException 通常意味着您传递的属性之一在模型中受到保护。
标签: php laravel laravel-5.5