If you want to generate some DB column value when creating record, one of the ways is to add it to model's boot() method.
For example, if you have a field "position" and want to assign the next available position to the new record (like Country::max('position') + 1), do this:
class Country extends Model { protected static function boot() { parent::boot(); Country::creating(function($model) { $model->position = Country::max('position') + 1; }); }}
Another option is to create an Observer class, with creating() method inside it.
Enjoyed This Tip?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses
[NEW] Building a Typical Laravel SaaS
8 lessons
1 h 07 min
PhpStorm Junie AI for Laravel Projects: Crash Course
7 lessons
36 min
NativePHP: Build Mobile App with Laravel
11 lessons
2 h 2 min read