简体   繁体   English

将 [title] 添加到可填充属性以允许在 [App\\Post] 上进行批量分配

[英]Add [title] to fillable property to allow mass assignment on [App\Post]

While inserting data in Mysql I have encountered the following error:在Mysql中插入数据时遇到以下错误:

"Add [title] to the fillable property to allow mass assignment on [App\\Post]." “将 [title] 添加到可填充属性以允许在 [App\\Post] 上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它工作正常:以下代码工作正常:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
   

Could anyone explain why an upper portion of the code is throwing an error?谁能解释为什么代码的上半部分会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

While inserting data in Mysql I have encountered with the following error:在Mysql中插入数据时,遇到以下错误:

"Add [title] to fillable property to allow mass assignment on [App\\Post]." “将[title]添加到可填充属性,以允许在[App \\ Post]上进行批量分配。”

Here is my code:这是我的代码:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :当我使用另一种方式插入数据时,它可以正常工作:以下代码可以正常工作:

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();

Could anyone explain why upper portion of code is throwing an error?谁能解释为什么代码的上部会抛出错误?

防止一个领域的最佳选择

Post::create($request->except('_token'));

Check the docs here检查这里的文档

Alternatively, you may use the create method to "save" a new model using a single PHP statement.或者,您可以使用 create 方法使用单个 PHP 语句“保存”新模型。 The inserted model instance will be returned to you by the create method:插入的模型实例将通过 create 方法返回给您:

use App\Models\Flight;

$flight = Flight::create([
    'name' => 'London to Paris',
]);

However, before using the create method, you will need to specify either a fillable or guarded property on your model class.但是,在使用 create 方法之前,您需要在模型类上指定可填充或受保护的属性。 These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.这些属性是必需的,因为默认情况下,所有 Eloquent 模型都受到保护,免受大规模分配漏洞的影响。

为模型 Post 中的可填充数组添加标题。

protected $fillable = ['title'];

I'm late to the party, but I had this problem and the solution was completely different.我迟到了,但我遇到了这个问题,解决方案完全不同。

Inside my PostController I was checking auth, then finding:在我的 PostController 中,我正在检查身份验证,然后发现:

Post::where('slug', $oldSlug)->first()->update([
            'image_url' => $request->image_url,
            'slug' => $newSlug,
            'title' => $request->title,
            'body' => $request->body,
            'description' => $request->description,
            'user_id' => auth()->user()->id,
            'catagory' => $request->catagory,
        ]);

I was looking at it trying to understand why it didn't like "image_url", screaming "Add ['image_url'] to fillable property to allow mass assignment".我看着它试图理解为什么它不喜欢“image_url”,尖叫着“将 ['image_url'] 添加到可填充属性以允许批量分配”。

I realized that I added the ->first() to make sure I was only getting one post, but since all slugs are validated and verified to be unique, I dropped the ->first() .我意识到我添加了->first()以确保我只收到一个帖子,但是由于所有 slug 都经过验证和验证是唯一的,我放弃了->first()

Post::where('slug', $oldSlug)->update([
        'image_url' => $request->image_url,
        'slug' => $newSlug,
        'title' => $request->title,
        'body' => $request->body,
        'description' => $request->description,
        'user_id' => auth()->user()->id,
        'catagory' => $request->catagory,
    ]);

Like magic, it works.就像魔术一样,它起作用了。

If anyone with more advanced knowledge can explain why it worked, I would be very interested in knowing.如果任何具有更高级知识的人都可以解释它为什么起作用,我会非常有兴趣知道。 Thanks!谢谢!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM