Trait that you want to add to a few Models to call their boot() method automatically

If you have a Trait that you want to add to a few Models to call their boot() method automatically, you can call Trait's method as boot[TraitName]

1class Transaction extends Model
2{
3 use MultiTenantModelTrait;
4}
1class Task extends Model
2{
3 use MultiTenantModelTrait;
4}
1trait MultiTenantModelTrait
2{
3 // This method's name is boot[TraitName]
4 // It will be auto-called as boot() of Transaction/Task
5 public static function bootMultiTenantModelTrait()
6 {
7 static::creating(function ($model) {
8 if (!$isAdmin) {
9 $isAdmin->created_by_id = auth()->id();
10 }
11 })
12 }
13}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord