php - Using Model Events Listener in Laravel 5 -


i make sure correctly used model events listeners in laravel 5 , didn't messed nothing (listener vs handler?). solution works fine, wonder if developed according concept , convention of laravel 5.

goal: set $issue->status_id on value when model saving.

in app\providers\eventserviceprovider.php

<?php namespace app\providers;  ...  class eventserviceprovider extends serviceprovider {      ...      public function boot(dispatchercontract $events)     {         parent::boot($events);          issue::saving('app\handlers\events\setissuestatus');     }  } 

in app\handlers\events\setissuestatus.php

<?php namespace app\handlers\events;  ...  class setissuestatus {      ...      public function handle(issue $issue)     {         if (something)         {                $issuestatus = issuestatus::where(somethingelse)->firstorfail();         }         else          {             $issuestatus = issuestatus::where(somethinganother)->firstorfail();         }          // issue_status() one-to-one relations issuetype (belongsto)         $issue->issue_status()->associate($issuestatus);     }  } 

thank time.

as said have working version , it's valid one, that's figure out if it's ok you.

just clarify i'm not saying these better solutions, valid different way.

since doing specific issue model or @ least doesn't seem generic event, set on model directly

<?php namespace app;  use illuminate\database\eloquent\model; use issuestatus;  class issue extends model {       protected static function boot()     {         parent::boot();          static::saving(function($issue){             if (something)             {                    $issuestatus = issuestatus::where(somethingelse)->firstorfail();             }             else              {                 $issuestatus = issuestatus::where(somethinganother)->firstorfail();             }              // issue_status() one-to-one relations issuetype (belongsto)             $issue->issue_status()->associate($issuestatus);          });     } } 

but if event indeed generic 1 , want use across multiple models, achieve same thing. need extract code model , use traits (like soft deletes)

first create our trait(in case created on root of our app) , extract code, wrote before, model:

<?php namespace app  use issuestatus;  trait issuestatussetter {     protected static function boot()     {         parent::boot();          static::saving(function($model){             if (something)             {                    $issuestatus = issuestatus::where(somethingelse)->firstorfail();             }             else              {                 $issuestatus = issuestatus::where(somethinganother)->firstorfail();             }              // issue_status() one-to-one relations issuetype (belongsto)             $model->issue_status()->associate($issuestatus);          });     } } 

now on models want use it, import trait , declare it's use:

<?php namespace app;  use illuminate\database\eloquent\model; use issuestatussetter;  class issue extends model {      use issuestatussetter;  } 

now last option showed it's generic option have apply every model declaring it's use on top of model.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -