Laravel雄辩更新tinyint(布尔值)

dev*_*dev 2 php laravel eloquent laravel-5 laravel-5.3

我在更新雄辩的模型时遇到了一个奇怪的问题,我使用了一个复选框(1 || undefined)并在验证后修补了所有数据

    $input = $request->all();
    $service->update($input);
Run Code Online (Sandbox Code Playgroud)

我试图专门勾选复选框

    $input['active'] = ($request->has('active') && $input['active']) ? 1 : 0
Run Code Online (Sandbox Code Playgroud)

但它仍然不会影响数据库.

当我转储Request我可以看到active: 10但没有任何改变数据库update()

我做了快速测试和使用

    $service->active = ($request->has('active') && $input['active']) ? 1 : 0 ;
    $service->save();
Run Code Online (Sandbox Code Playgroud)

做了这个工作.但为什么update()不更新这个领域呢?

Ale*_*nin 16

如果$request->active返回true,但它仍然没有保存在DB中,我打赌你忘了添加active$fillable数组:

protected $fillable = ['something', 'something_else', 'active'];
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/docs/5.3/eloquent#mass-assignment

  • 就是这样..我虽然我把它们都加了。 (2认同)
  • @VinayKaithwas 你无法轻松地与 $fillable 数组中不存在的列进行交互。 (2认同)