When i create a model with eloquent and i wan't to use boot or booted method static for trigger an action, is never triggered. Laravel 12 Eloquent
<?php
namespace App\Models;
use App\Traits\MeiliSearchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Fiche extends Model
{
use MeiliSearchable;
protected $table = 'fiches';
protected $guarded = ['id'];
protected $casts = [
'cdp_only' => 'boolean',
'documents' => 'array',
'description_editorjs' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected static function booted()
{
static::creating(function ($user) {
var_dump('Nouvelle creation');
});
}
}
You're calling $user
, which doesn't make impact in a Fiche
model. It should be:
Replace your boot method as:
static::creating(function ($fiche) {
var_dump('Nouvelle creation');
});
Don't use var_dump()
in Laravel models unless debugging from the CLI. Use Log::info()
or dd()
for testing:-
protected static function booted()
{
static::creating(function ($fiche) {
\Log::info('Nouvelle creation de fiche'); // Better than var_dump
});
}