Add default Eloquent Model Values on Laravel 9
Sometimes you forgot to add default values for table columns when creating the Laravel migrations, but fortunately, there is a cool feature of Laravel, you can add default values of the model fields every time you save a record.
Set the default values
You can use the $attributes array to set the default values for your eloquent model.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'title' => 'this is a default title',
'body' => 'this is a default body',
];
}