DB Models and EloquentTIP 52 / 100
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]
class Transaction extends Model
{
use MultiTenantModelTrait;
}
class Task extends Model
{
use MultiTenantModelTrait;
}
trait MultiTenantModelTrait
{
// Name the method boot[TraitName].
// Laravel calls it automatically when booting the model that uses the trait.
public static function bootMultiTenantModelTrait()
{
static::creating(function ($model) {
if (!$isAdmin) {
$isAdmin->created_by_id = auth()->id();
}
})
}
}