Laravel - Getting Started - Mass Assignment

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

A mass assignment vulnerability occurs when a user passes an unexpected HTTP request field and that field changes a column in your database that you did not expect. For example, a malicious user might send an is_admin parameter through an HTTP request, which is then passed to your model's create method, allowing the user to escalate themselves to an administrator.

So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model. For example, let's make the name attribute of our Flight model mass assignable:

    
    

Once you have specified which attributes are mass assignable, you may use the create method to insert a new record in the database. The create method returns the newly created model instance:

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

If you already have a model instance, you may use the fill method to populate it with an array of attributes:

    
    $flight->fill(['name' => 'Amsterdam to Frankfurt']);