Database Eloquent ORM Creating data and configuring mass assignment

First add a create method:

use App\Models\Post;

/*
|--------------------------------------------------------------------------
| ELOQUENT ORM (Object Relational Model)
|--------------------------------------------------------------------------
*/

Route::get('/create', function(){
    Post::create(['title'=>'the create method', 'content'=>'learning about create method']);
});

Now check URL. You should get an error like so:

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

To allow us to save multiple items of data you need to edit your model with this inside the class:

protected $fillable = [
    'title',
    'content'
];

If you check the URL again you should get an error free page and if you check phpmyadmin, you’ll see a new record.

Leave a Reply

Your email address will not be published. Required fields are marked *