khoanguyenme's avatar

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?

0 likes
11 replies
pmall's avatar

Shouldn't your event handler implements ShouldBeQueued ? Serializing models has no sense if the event isn't queued

khoanguyenme's avatar

@pmall sorry about that. edited. I have another handler class which will be queued. But the same thing happens.

pmall's avatar

I've no idea about this.

But why using model observer and not a command to handle this ? It seems a bit unnatural to use a model observer to fire an event.

khoanguyenme's avatar

@pmall I know. But that's not a problem right? I tried place it in my command, controller, but the same result. I have to get model from database (from the given id).

usman's avatar

@khoanguyenme when handlers having Models as type hints are queued Laravel only serialize the Model Id. when the job/handler is actually handled, it retrieves the model from database using the id. I am using the pheanstalk ~3.0 and I am unable to reproduce your problem.

khoanguyenme's avatar

@usman It weirds for me too. It works on a old model item. But not on the newly created model item. I'm trying to reproduce this on an fresh install. I'm using redis for queue.

pmall's avatar

Isn't it the event that should implements ShouldBeQueued and not the event handler ?

isaackearl's avatar

@khoanguyenme

Any update on this? .. I'm having the exact same problem.. I want to use a model in my listeners but it only returns ModelIdentifier.

Kryptonit3's avatar

what about

class Upload  extends Model  {

public static function boot()
    {
        parent::boot();

        static::created(function($upload){
            event (new NewFileUploaded($upload));
        });
    }
}
ktnam's avatar

It works when taken out SerializesModels trait from Event class.

Please or to participate in this conversation.