
If you are coming from Java world and Hibernate, this is Hibernate of PHP and Laravel. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. Laravel's documetation introduction to Eloquent: So we add it to our Article model and return an array of settings (in our abstract definition we’ve also required that the method returns an array).In all our apps, at some point or another, we need to save a record to the database, luckily Laravel helps us tremendously using Eloquent. With this abstract method now defined any class that uses the trait will need to have this method defined otherwise an error will be thrown. abstract public function sluggable(): array So let’s add a requirement to our trait which we’ll just name after it (you can call it anything you like). To ensure that this method exists we can define an abstract method in our trait. One way we can do this is by defining a method in each class exhibiting the trait that will return an array of settings. We need to add a way of configuring our trait. For example, we might also have a Category model that we want to attach the trait to that has a name instead of a title. So far we’ve assumed that our slug will always be generated from the model’s title property, but what if we need to use something else. It will just get applied to each model we attach the trait to.

With our trait now bootable we can get rid of the generateSlug call from our model’s boot method. If you want to look at Laravel’s sourcecode to see what’s going on check out the bootTraits method in \Illuminate\Database\Eloquent\Model. This array can then be used to see if the trait has added the boot method to the class and if the relevant method exists it calls it. Under the hood Laravel is using the PHP method class_uses which returns an array containing the names of all the traits of the given class. The boot method of each associated trait will get called at the same time as the model’s boot method. So you can hook in to any of the Eloquent events from here. The trait’s boot method works just like an Eloquent model’s boot method. slug = $model->generateSlug($model->title)
#Eloquent events code#
In this method we can add the code we already have for generating the slug on save that we previously wrote directly in our Article model. So for our example Sluggable trait the method would be named bootSluggable. You can boot your traits by adding a boot method to it. In Laravel, if we use a trait with an Eloquent model we get some extra magic thrown in. Thankfully Laravel has a solution for this, we can boot Eloquent traits! Booting Eloquent Traits However, we don’t really want to have to add all this code to each model we add the trait to in order to hook into the saving event.

Now the article’s slug property is being set on save. So let’s get started by defining a Sluggable trait with a simple method for generating a slug from a string.

For example, Colin Viebrock’s Eloquent Sluggable There’s no point re-inventing the wheel! A Simple Trait Example If you are here wanting to incorporate a sluggable behaviour into your own app I’d suggest installing one of the many existing packages out there that will do this for you. To take a look at how we can use traits with our Eloquent models we’ll consider a real world example and look under the hood to see how Laravel enables us to boot traits.įor our example we’ll create a basic trait for adding a sluggable behaviour to models that generates SEO friendly URL slugs.īefore we continue, the following example is just to demonstrate a practical use of traits with Laravel. Laravel’s own SoftDeletes trait uses this bootable feature to add a global scope that allows us to soft delete a model. When using traits with Eloquent models, Laravel has a neat trick that boots the trait allowing us to hook into the Eloquent events. They also allow us to pick ‘n’ mix behaviours for classes in a way that class inheritance doesn’t. Any properties or methods of a trait become instantly available to any class we attach the trait to as if they were properties or methods of that class. They provide a way of horizontally sharing logic between classes.

Traits are a useful means of writing DRY code. In this blog post we’ll take a look at using PHP traits with Laravel’s Eloquent models, hooking them up with events and making them configurable.
