deshiloh's avatar

Upload store file return wrong path

Hi ! I'm trying to upload a image file, that work, but my problem is in the documentation, i read that the UploadedFile store method return the path where file was uploaded.

Here my script :

public function store(StoreLunch $request)
    {
        $request->validated();
        if ($request->hasFile('picture')) {
            $request->merge([
                'picture' => $request->file('picture')->store('lunches', 'public')
            ]);
        }
        Lunch::create($request->except(['_token']));
        return redirect()->route('lunches.index')->with('success', "Lunch created !");
    }

In the database the value i have :

/tmp/phpe1pHmK

Am I doing something wrong ?

0 likes
4 replies
Nakov's avatar

@deshiloh I believe you are doing it right, but have you checked it the merge works properly?

What if you try before the create on the model to dump dd($request->except(['_token']));?

Or try replacing merge with add.

deshiloh's avatar
deshiloh
OP
Best Answer
Level 1

@nakov

Ok the dd give me the right path, my $request doesn't have add method, perhaps because it's a custom request.

The problem was the method tool the UploadedFile and not the path i gave, i didn't find any method to replace the picture in $request

so i did like this :

public function store(StoreLunch $request)
    {
        $request->validated();
        $datas = $request->except(['_token', 'picture']);
        if ($request->hasFile('picture')) {
            $datas['picture'] = $request->file('picture')->store('lunches');
        }
        Lunch::create($datas);
        return redirect()->route('lunches.index')->with('success', "Lunch created !");
    }

It worked but i have the feeling i do that in a dirty way ^^'

Nakov's avatar

@deshiloh I don't see anything dirty in this approach :) I'll do the same :)

Please or to participate in this conversation.