Laravel 5.5 MassAssignmentException
Laravel 5.5 MassAssignmentException
我正在学习 Laravel From Scratch 教程系列,我目前正在为您的文章系统创建评论系统。但是我遇到了一个问题,我现在真的不知道错误在说什么。
错误:
Illuminate\Database\Eloquent\MassAssignmentException
body
评论模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
post型号:
<?php
namespace App;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
}
我走的路线:
Route::post('/posts/{post}/comments', 'CommentsController@store');
评论管理员:
<?php
namespace App\Http\Controllers;
use App\Post;
class CommentsController extends Controller
{
public function store(Post $post)
{
$post->addComment(request('body'));
return back();
}
}
提前致谢!
批量赋值是当您将数组发送到模型创建时,基本上是一次在模型上设置一堆字段,而不是一个一个地设置,就像您在此处所做的那样:
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
您需要将要填充的字段添加到 Comments.php
模型中的可填充数组:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['body'];
public function post()
{
return $this->belongsTo(Post::class);
}
}
如文档所述:
You may also use the create method to save a new model in a single
line. The inserted model instance will be returned to you from the
method. However, before doing so, you will need to specify either a
fillable or guarded attribute on the model, as all Eloquent models
protect against mass-assignment by default.
希望对您有所帮助。
这个错误的解释
这是 Laravel 的安全功能。它旨在保护您在使用批量分配时免受表单操纵。
例如在注册表单上:当您的数据库中有一个 is_admin
列时,用户只需操作您的表单即可将 is_admin
设置为 true
服务器,因此在您的数据库中。此安全功能通过使用白名单定义安全字段来防止这种情况。
如何解决
您需要在模型上设置 $fillable
属性。它的值必须是一个数组,其中包含可以安全地批量分配的所有字段(例如用户名、电子邮件地址……)。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
# This property!
protected $fillable = ['body'];
// ...
}
请参阅文档中的 "Mass assignment":
https://laravel.com/docs/5.5/eloquent#mass-assignment
我正在学习 Laravel From Scratch 教程系列,我目前正在为您的文章系统创建评论系统。但是我遇到了一个问题,我现在真的不知道错误在说什么。
错误:
Illuminate\Database\Eloquent\MassAssignmentException
body
评论模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}
post型号:
<?php
namespace App;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
}
我走的路线:
Route::post('/posts/{post}/comments', 'CommentsController@store');
评论管理员:
<?php
namespace App\Http\Controllers;
use App\Post;
class CommentsController extends Controller
{
public function store(Post $post)
{
$post->addComment(request('body'));
return back();
}
}
提前致谢!
批量赋值是当您将数组发送到模型创建时,基本上是一次在模型上设置一堆字段,而不是一个一个地设置,就像您在此处所做的那样:
public function addComment($body)
{
$this->comments()->create(compact('body'));
}
您需要将要填充的字段添加到 Comments.php
模型中的可填充数组:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['body'];
public function post()
{
return $this->belongsTo(Post::class);
}
}
如文档所述:
You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.
希望对您有所帮助。
这个错误的解释
这是 Laravel 的安全功能。它旨在保护您在使用批量分配时免受表单操纵。
例如在注册表单上:当您的数据库中有一个 is_admin
列时,用户只需操作您的表单即可将 is_admin
设置为 true
服务器,因此在您的数据库中。此安全功能通过使用白名单定义安全字段来防止这种情况。
如何解决
您需要在模型上设置 $fillable
属性。它的值必须是一个数组,其中包含可以安全地批量分配的所有字段(例如用户名、电子邮件地址……)。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
# This property!
protected $fillable = ['body'];
// ...
}
请参阅文档中的 "Mass assignment": https://laravel.com/docs/5.5/eloquent#mass-assignment