简体   繁体   中英

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

While inserting data in Mysql I have encountered the following error:

"Add [title] to the fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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:

"Add [title] to fillable property to allow mass assignment on [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. The inserted model instance will be returned to you by the create method:

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. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.

为模型 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:

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".

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() .

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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