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. The Eloquent all method will return all of the results in the model's table.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. Now, let's look at an example Flight model, which we will use to retrieve and store information from our flights database table: name If you would like to generate a database migration when you generate the model, you may use the -migration or -m option: php artisan make:model User -migration
The easiest way to create a model instance is using the make:model Artisan command: php artisan make:model User
All Eloquent models extend Illuminate\Database\Eloquent\Model class.
Eloquent events free#
Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file.
To get started, let's create an Eloquent model. For more information on configuring your database, check out the documentation. Models allow you to query for data in your tables, as well as insert new records into the table.īefore getting started, be sure to configure a database connection in config/database.php. Each database table has a corresponding "Model" which is used to interact with that table. The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database.