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
        });
    }
}

No comments yet

Leave your comment

Search
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the Bootstrap 5 card component!