marian0's avatar

Why I can't access the Request files on an Eloquent Callback ?

Hello guys,

I was trying to do some upload tasks on an eloquent callback (tried with saved and saving) but I couldn't because somehow the file is deleted when the callback is executed.

I got :

File `/Applications/MAMP/tmp/php/phpUpH4Q5` does not exist

But it's the same callback. The way I'm doing that is like:

 protected static function boot()
    {
        self::saving(function ($content) {

            if (Input::file('image')) {
                $content->clearMediaCollection();

                $content->addMedia(Input::file('image'))
                    ->toMediaCollection();
            }
        });
    }

I think this is a pretty simple approach to get files being uploaded in an encapsulated, reusable and simple way.

Do you know how to solve that or maybe suggest me some others approach that work for you for handling file uploads for models ( Creation + Edit ).

Thank you!

0 likes
2 replies
devonblzx's avatar

I wouldn't place logic relating to an HTTP request in your Model.

Beyond that, it looks like maybe you've already moved the file? Hard to know without seeing more.

marian0's avatar

Thanks for your reply.

The thing is quite simple: I have a model and it has a related picture. I planned to store that picture "aftersaving" the entity if there is any image file in the request. For the moment is a good approach for me but let me know your thoughts about that:

  • Which approach should I use o do you recommend ?
  • Do you have any code example?

Thank you!

Please or to participate in this conversation.