@herminia_bruen
In Laravel, you can use the fillable
property in your Eloquent model to specify which fields are allowed to be mass assigned. This will allow you to automatically fill in the fields when creating or updating a model.
Here's an example of how you can use the fillable
property in your Eloquent model:
1 2 3 4 5 6 7 8 |
namespace App; use IlluminateDatabaseEloquentModel; class User extends Model { protected $fillable = ['name', 'email', 'password']; } |
In this example, the fillable
property is used to specify that only the name
, email
, and password
fields are allowed to be mass assigned. This means that when creating or updating a User
model, you can pass an array of data that includes these fields, and Laravel will automatically fill them in.
Here's an example of how you can create a new User
model and automatically fill in the fields using the create
method:
1 2 3 4 5 |
$user = User::create([ 'name' => 'John Doe', 'email' => '[email protected]', 'password' => 'password123' ]); |
In this example, the create
method takes an array of data as an argument, and Laravel automatically fills in the name
, email
, and password
fields when creating the new User
model.
Alternatively, you can also use the update
method to automatically fill in the fields when updating an existing model:
1 2 3 4 5 |
$user = User::find(1); $user->update([ 'name' => 'Jane Doe', 'email' => '[email protected]' ]); |
In this example, the update
method takes an array of data as an argument, and Laravel automatically fills in the name
and email
fields when updating the existing User
model.
By using the fillable
property in your Eloquent model, you can easily autofill fields when creating or updating models in Laravel.