【发布时间】: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