简体   繁体   English

“将 [name] 添加到可填充属性以允许对 [Illuminate\Foundation\Auth\User] 进行批量分配。”

[英]"Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User]."

User.php code, here, whether I use fillable or gaurded, I get the same error. User.php代码,这里不管我用fillable还是gaurded,都报一样的错。

<?php

namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     *@var array
     */
    // protected $fillable = [
    //     'name',
    //     'email', 
    //     'password',
    // ];
    
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

UserController.php code, here, I have tried the mass assignment UserController.php代码,这里我试过批量赋值

<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;




class UserController extends Controller
{
    public function index()
    {
        $data = [
            'name' => 'elon',
            'email' => 'elon@gmail.com',
            'password' => 'password',
        ];

        User::create($data);

        $user = User::all();
        return $user;
    }
}

You seem to not be importing the user class from the right namespace in your UserController.php您似乎没有从 UserController.php 中的正确命名空间导入用户 class

You are using您正在使用

use Illuminate\Foundation\Auth\User;

Use利用

use App\Models\User;

instead.反而。

Edit:编辑:

$fillable is not the problem in this case as $guarded is set to an empty array which allows for all fields to be mass assignable through the create method. $fillable 在这种情况下不是问题,因为 $guarded 设置为一个空数组,它允许所有字段都可以通过 create 方法进行批量分配。 Eloquent mass assignment Eloquent 质量分配

There are two problems in the code provided:提供的代码有两个问题:

  1. As commented by @sta, you should allow the Model attributes to be mass assignable by using the $fillable property in the User class:正如@sta 评论的那样,您应该允许通过使用User class 中的$fillable Model属性来批量分配 Model 属性:
<?php

namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     *@var array
     */
     protected $fillable = [
         'name',
         'email', 
         'password',
     ];
    
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}
  1. As commented by @Remy, we should make sure to use the correct class :正如@Remy 评论的那样,我们应该确保使用正确的class
<?php

namespace App\Http\Controllers;

use App\Models\User; // <-- corrected line

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class UserController extends Controller
{
    public function index()
    {
        $data = [
            'name' => 'elon',
            'email' => 'elon@gmail.com',
            'password' => 'password',
        ];

        User::create($data);

        $user = User::all();
        return $user;
    }
}

in the UserController.php try to useUserController.php尝试使用

use App\Models\User;

in laravel 7 this work for me:在 laravel 7 这对我有用:

use App\User;

For me, I had to stop the server in terminal with ctrl + c and the restart the server with php artisan serve It worked for me.对我来说,我必须使用ctrl + c停止终端中的服务器,并使用php artisan serve重新启动服务器它对我有用。

I found this helpful for me in laravel 8, this worked fine in all versions because many times if we import class and it auto import another one so please check that you import this class or another one.我发现这在 laravel 8 中对我有帮助,这在所有版本中都可以正常工作,因为如果我们多次导入 class 并且它会自动导入另一个,所以请检查您是否导入了这个 class 或另一个。

use App\Models\User;

by adding this in my model通过在我的 model 中添加这个

protected $guarded = [];受保护的 $guarded = [];

it save me from my misery thanks!它使我免于痛苦,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 [username] 添加到可填充属性以允许在 [App/post] 上进行批量分配 - Add [username] to fillable property to allow mass assignment on [App/post] 将 [title] 添加到可填充属性以允许在 [App\\Post] 上进行批量分配 - Add [title] to fillable property to allow mass assignment on [App\Post] 找不到类&#39;Illuminate \\ Foundation \\ Auth \\ User&#39;JWT Auth Laravel - Class 'Illuminate\Foundation\Auth\User' not found JWT Auth Laravel Laravel 4雄辩的批量分配错误 - Laravel 4 eloquent Mass assignment error in fillable 在不可填写的字段上进行批量分配 - Mass assignment working on non-fillable fields Laravel不遵守相关模型更新的批量分配$可填充规则 - Laravel not adhering to Mass Assignment $fillable rules for related model update Laravel 5.2错误App \\ User无法使用Illuminate \\ Foundation \\ Auth \\ User - 它不是特征 - Laravel 5.2 Error App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait Illuminate\\Foundation\\Auth\\ 中不存在 AuthenticatesUsers 文件 - AuthenticatesUsers file does not exist in Illuminate\Foundation\Auth\ 类&#39;Illuminate \\ Foundation \\ Auth错误Laravel 5.4 - Class 'Illuminate\Foundation\Auth Error Laravel 5.4 Model class 在 Illuminate\Foundation\Auth\Vendor 中找不到 - Model class not found in Illuminate\Foundation\Auth\Vendor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM