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) 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.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |