Shouldn't your event handler implements ShouldBeQueued ? Serializing models has no sense if the event isn't queued
Feb 19, 2015
11
Level 3
L5 Event and model serialize
I want to do some actions with uploaded file (like resize, backup to S3, .... )
In my controller. After a file was uploaded, a infomation of that file will be written into database through Upload model.
I call a event inside Upload model like this
class Upload extends Model {
public static function boot()
{
parent::boot();
Upload::created(function($upload){
event (new NewFileUploaded($upload));
});
}
}
Here is my event class:
<?php namespace Quiz\Events;
use Quiz\Events\Event;
use Illuminate\Queue\SerializesModels;
use Quiz\Models\Upload;
class NewFileUploaded extends Event {
use SerializesModels;
/**
* @var Upload
*/
public $upload;
/**
* Create a new event instance.
*
* @param Upload $upload
* @return \Quiz\Events\NewFileUploaded
*/
public function __construct(Upload $upload)
{
$this->upload = $upload;
}
}
My Event handler :
<?php namespace Quiz\Handlers\Events;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use Quiz\lib\Repositories\Upload\UploadRepository as Upload;
class RotateImage implements ShouldBeQueued {
/**
* Handle the event.
*
* @param NewFileUploaded $event
* @return void
*/
public function handle(NewFileUploaded $event)
{
$upload = $event->upload;
dd($upload);
}
}
And all I got is an instance of ModelIdentifier class with the correct model name and id but not the model itself.
Where am I doing wrong?
Please or to participate in this conversation.