微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 在空字段中更新或插入值

如何解决Laravel 在空字段中更新或插入值

我在空字段中插入值时遇到问题。

这是桌子

public function up()
    {
        Schema::create('users',function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('username')->unique()->nullable();
            $table->string('mobile_no')->nullable();
            $table->string('address')->nullable();
            $table->string('email')->unique();
            $table->boolean('role_name')->default(0);
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

这是用户控制器

public function update(Request $request,User $user)
    {
        $request->validate([
            'name' => 'required|max:255','mobile_no' => 'required|max:11','address' => 'required|max:255',]);
        $user->update($request->all());
        return redirect()->route('profile')->with('updated','Profile successfully updated');
    }

我没有收到错误消息,$message 总是说:“配置文件已成功更新”,但它并没有真正更新。

解决方法

您是否在模型中声明该字段可填写?

    protected $fillable = [
    'name','username',.....
];

https://laravel.com/docs/8.x/eloquent#inserting-and-updating-models

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。