Loading models, traits

Original author: Simon Archer
  • Transfer
So, today I recognized a small fitra Laravel / Eloquent , which is practically not described and is only mentioned in passing in the documentation of the framework.

Go to TL; DR if you just want to see this feature.

You may already know that you can add static boot () methods to Eloquent models that will be executed when it boots. This can be used, for example, snapping to model events, if necessary. For example, you can organize an email notification each time a new user is created ( unsuccessful example ), but you could describe it as follows:
    class User extends Eloquent {
        public static function boot()
        {
            parent::boot();
            static::created(function($user) {
                // Send a mailing…
            });
        }
    }

But what if you want to put this with trait ?

Consider this scenario; It is necessary to organize a search in a range of models. Of course, you can create a new SearchableModel class , inheriting it from Eloquent \ Model , but I would like to write in such a way that the functionality is easily portable to other projects without being tied to a specific application. The trait here will be better after all, because it is easily portable and relatively unobtrusive, connecting which you get additional functionality that can be easily redefined.

So, I created SearchableTrait.
     trait SearchableTrait {
         public function search($query)
         {
             // ...
         } 
    }

Quite simply for now, we call the $ model-> search ('query') method , which returns models matching the query criteria.

Nevertheless, instead of directly searching the database, I plan to use a third-party application search ( if you are interested, elasticsearch ), using its own indexing, which requires manually adjusting the index of each row.

But where do I paste this code?

I could put it in the boot () method of the model, but there it can be overwritten, thereby disrupting my search and destroying any chance of being reused.

TL DR

The guys from Laravel explicitly provided for how to load your traits, defining a special method, as in the Eloquent model, for example:
    trait SearchableTrait {
        public function search($query)
        {
            // ...
        }
        public static function bootSearchableTrait()
        {
            static::created(function($item){
                // Index the item
             });
        }
    }

Thus, there is a little classical magic Eloquent . If you have a static method in tray, which is called according to the principle of boot [ NameOfTrait] , then it will be executed in the same way as the static boot () method of the model. What a convenient place to register the events of your model.

An example in the Laravel documentation, implements this trick when registering a global scope , to intercept all running requests, while deleting models softly .

Of course, in addition to this feature, there are other good ways to achieve the same result, but it is always nice to keep poorly documented subtleties in your arsenal, to use them if necessary.

Also popular now: