我在更新 eloquent 模型时遇到了一个奇怪的问题,我使用了一个复选框 (1 || undefined ) 并在使用验证后修补所有数据
$input = $request->all();
$service->update($input);
我试图专门检查复选框$input['active'] = ($request->has('active') && $input['active']) ? 1 : 0
但它仍然不会影响数据库。当我转储
Request
时,我可以看到 active:
1
或 0
但这些都不会改变 update()
上的数据库我做了一个快速测试和使用
$service->active = ($request->has('active') && $input['active']) ? 1 : 0 ;
$service->save();
完成了工作。但是为什么 update()
没有更新这个字段? 最佳答案
如果$request->active
返回true,但仍未保存在数据库中,我敢打赌您忘记将active
添加到$fillable
数组中:
protected $fillable = ['something', 'something_else', 'active'];
https://laravel.com/docs/5.3/eloquent#mass-assignment
关于php - Laravel Eloquent 更新 tinyint (boolean),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40212500/