【问题标题】:When trying to insert data got this error msg Add [name] to fillable property to allow mass assignment on [App\Suitspecialist]尝试插入数据时收到此错误消息将 [name] 添加到可填充属性以允许在 [App\Suitspecialist] 上进行批量分配
【发布时间】:2020-07-12 21:32:29
【问题描述】:

谁能解释一下为什么控制器部分会抛出错误?

这是我的模型:

class Suitspecialist extends Authenticatable
{
    use Notifiable;
    protected $guard = 'suitspecialist';
    protected $fillable = [
        'name', 'email', 'password',
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

控制器

这部分会抛出错误

将 [name] 添加到可填充属性以允许在 [App\Suitspecialist] 上进行批量分配。

protected function createSuitspecialist(Request $request)
{
    $this->validator($request->all())->validate();
    Suitspecialist::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]);
    return redirect()->intended('login/suitspecialist');
}

【问题讨论】:

  • 您可能需要清除缓存:运行composer dump-autoloadcomposer clear-cachephp artisan cache:clear,甚至可能是php artisan route:clear
  • 我试过了,还是报同样的错误
  • 删除protected $guard = 'suitspecialist'试试
  • 好的,只是为了确保:您在这个项目中只有 1 个 Suitspecialist 课程?它在 App 命名空间/文件夹下?
  • 谢谢罗伯!我才发现

标签: laravel post get laravel-6


【解决方案1】:

您不能同时使用guardedfillable。最好只使用fillable

【讨论】:

    【解决方案2】:

    当您尝试使用填充、创建或更新等方法填充模型的重要属性时,您需要指定可以通过这种方式填充的字段。这称为“批量分配”。

    这是一种 Eloquent 安全措施,可避免您存储不想要的数据。

    在您的模型中,使用guardedfillable 属性来指定您想要或不想要注册使用质量分配的属性。这些属性接受一个数组。

    查看 Laravel 文档,它的解释很好:https://laravel.com/docs/7.x/eloquent#mass-assignment

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2020-05-02
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多