Add default value to Models
Posted: 2025/03/07
Updated: 2025/03/10
Using this to add default value to the Models, of which does not available in the submitted form.
class Task extends Model
{
protected static function boot()
{
parent::boot();
static::creating(function ($task) {
if (is_null($task->completed)) {
$task->completed = false; // Ensure completed is false when creating a new task
}
$task->user_id = Auth::id(); // Set user_id automatically
});
}
}