It’s quite open-ended as to where to register listeners for model events. One place is in the boot
method within the EventServiceProvider
class:
public function boot(DispatcherContract $events) { parent::boot($events); User::creating(function($user) { // Do something }); }
Be sure to import the namespace for the DispatcherContract
at the top of the file:
use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract;
Eloquent models provide a method for each event that you can pass an anonymous function to. This anonymous function receives an instance of the model that you can then act upon. So if you wanted to create a URL-friendly representation of an article headline each time your Article
model was saved, you can do this by listening on the saving event:
Article::saving(function($article) { $article->slug = Str::slug($article->headline); });
IT is more important than ever in the world of higher education, and yet with…
If you’re here for the top tips, we assume you’re ahead of the “how to…
The world is progressing at unprecedented rates at the current moment, especially in terms of…
This article will highlight the Top 20 Opensource Python Tkinter Projects which we believe will…
With their numerous applications in streamlining the data flow, securing both the servers and the…
In this article, We will be looking at some of the top Node.js dashboard templates.…