【问题标题】:"Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User]."“将 [name] 添加到可填充属性以允许对 [Illuminate\Foundation\Auth\User] 进行批量分配。”
【发布时间】:2020-12-28 17:05:11
【问题描述】:

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 代码, 在这里,我尝试了批量分配

<?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;
    }
}

【问题讨论】:

  • 取消注释 User 模型上的可填写代码

标签: php laravel laravel-8 mass-assignment


【解决方案1】:

您似乎没有从 UserController.php 中正确的命名空间导入用户类

你正在使用

use Illuminate\Foundation\Auth\User;

使用

use App\Models\User;

改为。

编辑:

$fillable 在这种情况下不是问题,因为 $guarded 设置为一个空数组,它允许通过 create 方法对所有字段进行批量分配。 Eloquent mass assignment

【讨论】:

    【解决方案2】:

    提供的代码有两个问题:

    1. 正如@sta 所评论的,您应该允许Model 属性可以通过在$fillable 类中使用$fillable 属性进行批量分配:
    <?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. 正如@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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我发现这在 laravel 8 中对我很有帮助,这在所有版本中都运行良好,因为很多时候如果我们导入类并且它会自动导入另一个类,所以请检查你是导入这个类还是另一个类。

      use App\Models\User;
      

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人可以从中学习
      【解决方案4】:

      UserController.php尝试使用

      use App\Models\User;
      

      【讨论】:

        【解决方案5】:

        在 laravel 7 中这对我有用:

        use App\User;
        

        【讨论】:

          【解决方案6】:

          对我来说,我必须用ctrl + c 停止终端中的服务器,然后用php artisan serve 重新启动服务器,这对我有用。

          【讨论】:

            【解决方案7】:

            通过在我的模型中添加它

            受保护的 $guarded = [];

            它把我从痛苦中解救出来,谢谢!

            【讨论】:

              猜你喜欢
              • 2019-05-16
              • 2020-05-02
              • 2021-08-24
              • 2020-07-12
              • 2017-05-03
              • 2018-02-11
              • 2020-07-16
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多