@darrion.kuhn
To add a custom event to a Laravel model, you can use Laravel's built-in event system. Here's how you can do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace App;
use IlluminateDatabaseEloquentModel;
use AppEventsCustomEvent;
class YourModel extends Model
{
protected $dispatchesEvents = [
'custom_event' => CustomEvent::class,
];
// Your model code here
}
|
1
|
php artisan make:event CustomEvent |
This will create a new event class under the App/Events directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
namespace AppEvents;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
class CustomEvent
{
use Dispatchable, SerializesModels;
public function __construct()
{
// Constructor logic here
}
}
|
1 2 3 |
use AppYourModel; YourModel::find(1)->event(new CustomEvent()); |
That's it! Now, when the custom_event is triggered on a YourModel instance, the CustomEvent class will be executed.