@Yado
in a simple language,
When you retrieve a record via Eloquent, you can access its properties as follows:
$country = Country::find(1);
print $country->name;
We can update attribute values in the same manner:
$country->name = 'INDIA';
This will set the value in the model instance, but we need to persist the change to the
database. We do this by calling the save method afterwards:
$country->save();
This also could be use for creating new record.
If you have a table with lots of columns, then it will become tiresome to assign
each property manually like this. Eloquent allows you to fill models
by passing an associative array with values, and the keys representing the column
names. You can fill a model while either creating or updating it:
$data = [
'name' => 'INDIA',
'area' => '12345',
'language' => 'hindi,
];
Country::create($data);
However, this will throw a MassAssignmentException error.
If the $data array in the
previous example came from say, a user's form submission, then they can update
any and all values in the same database.
Consider that you have a users table with a column called is_admin, which
determines whether or not that user can view your website's administration area.
Also consider that users on the public side of your website can update their profile.
If, during form submission, the user also included a field with the name of is_admin
and a value of 1, that would update the column value in the database table and grant
them access to your super secret admin area—this is a huge security concern and is
exactly what mass-assignment protection prevents.
To mark columns whose values are safe to set via mass-assignment (such as name,
birth_date, and so on.), we need to update our Eloquent models by providing a
new property called $fillable. This is simply an array containing the names of the
attributes that are safe to set via mass assignment:
class Country extends Model {
protected $fillable = [
'name',
'area',
'language',
];
}
In short save() method is used both for saving new model, and updating existing one.
here you are creating new model or find existing one, setting its properties one by one and finally saves in database,
while in create method you are passing array, setting properties in model and persists in database in one shot.