Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Armani's avatar
Level 17

FormRequest and upload file issue

I have a model that has a file name column, and I validated the incoming requests using FormRequest like this:

public function rules()
    {
        return [
            'photo' => 'image|mimes:jpeg,bmp,png|max:2048',
            'user_id' => 'required|numeric'
        ];
    }

    public function prepareForValidation()
    {
        $this->merge(['user_id' => auth()->id(), 'photo' => $this->checkPhoto()]);
    }

    private function checkPhoto()
    {
        if ($this->hasFile('photo')) {
            $image = request('photo');
            $name = request('code') . '.' . $image->getClientOriginalExtension();
            Image::make($image)->fit(591, 709)->save(public_path('/images/photos' . '/' . $name), 100);
            return $name;
        }
    }

And inside controller I used $request->validated() function:

Model::create($request->validated());

but the photo column would be "C:\wamp\tmp\phpBEED.tmp" and I know what is the issue, $request->photo gives us the Illuminate\Http\UploadedFile instance.

How to solve this issue?

0 likes
1 reply
vincent15000's avatar

I'm not sure that the form request is the best place to write the checkPhoto() method, that's not a validation rule.

Furthermore you are merging a simple string as photo, so the rules for the photo don't match (string vs image).

Please or to participate in this conversation.