keven1508's avatar

Storing image by a mutator in model, should or not? and mass assignment

Hello,

Background information

  1. a Common class contains a static saveImage method which simply stores an image and returns a filename.
  2. a User model that has profile_pic field as string
  3. a UserController controller with save method for handling data from user input

Questions:

Should I use mutator to handle file uploading

so that I can do something like this

in UserControler

public function save(Request $req) {
    $user = new User();
    $user->profile_pic = $req->file('profile');
}

in User model

public function setProfilePicAttributes($file) {
    $this->attributes['profile_pic'] = Common::saveImage($file);
}

Although the code could have some problems since I type it right away, this concept is working for me. However, I have never seen any tutorial or any project that used this approach, so is there any reason for not doing this? or simply what is the best practice in this case?

Would this approach make sense in mass assignments?

if the above approach is good, would it be very handy for using mass assignments? so that everything can be handled automatically.

Thank you :)

0 likes
5 replies
EnterUsername's avatar

Hi,

The answer is almost always "it depends". There is nothing wrong with using a mutator for this action. I have done it before and I have resorted to similar methods with large applications.

A bit OT:

It seems like you re-invented the wheel with your "Common"-class. Laravel provides a class called Storage that basically does the same thing but can support multiple storage solutions (local storage, dropbox, s3 etc).

I cant post links, but google "laravel storage" to read more :)

keven1508's avatar

Thanks for the answer.

the saveImage method is not really the same with Storage built-in method of Laravel. Of course, under the hood, it uses Storage, I am using my own saving method is because there are a few logic and tasks needed to be done as well.

1 like
martinbean's avatar

Should I use mutator to handle file uploading

@keven1508 Not really. That’s not what mutators are for at all.

keven1508's avatar

May I ask why not?

I am trying to avoid calling an upload method in controller (or do I need to avoid it?)

$user->profile_pic = Common::saveImage($req->file('profile'));

what is your suggestion?

Thanks.

martinbean's avatar

@keven1508 They’re just not a place for business logic at all. They shouldn’t have side effects, such as uploaded a file or interacting with a file system.

If you have logic outside of setting simple attributes on a model, then you could consider wrapping it up into an “action” class that you dispatch from your controller instead:

class CreateUser
{
    public function handle(array $attributes, UploadedFile $profilePic = null)
    {
        if ($profilePic) {
            $attributes['profile_pic'] = $profilePic->store('profile-pictures');
        }

        return User::create($attributes);
    }
}
public function store(StoreUserRequest $request, CreateUser $action)
{
    $user = $action->handle($request->input(), $request->file('profile_pic'));

    // Return response
}

Please or to participate in this conversation.