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;
}
}
最佳答案
您似乎没有从 UserController.php 中的正确命名空间导入用户类
您正在使用
use Illuminate\Foundation\Auth\User;
使用
use App\Models\User;
相反。
编辑:
$fillable 在这种情况下不是问题,因为 $guarded 被设置为一个空数组,它允许所有字段通过 create 方法进行批量分配。 Eloquent mass assignment
关于php - "Add [name] to fillable property to allow mass assignment on [Illuminate\Foundation\Auth\User].",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65481253/