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

V1MPAR's avatar

Serialization of 'Illuminate\Http\UploadedFile' is not allowed

Hey. The problem as in the subject. How can I solve it?

public function store(Request $request)
{
        // validation
        $validator = $request->validate([
            'title' => 'required|min:4|max:128',
            'description' => 'required|min:16',
            'min_price' => 'required|numeric',
            'date_end' => 'required',
            'img' => 'required|max:10000'
        ]);
        // clear inputs
        $title = e($request->title);
        $description = e($request->description);
        $min_price = e($request->min_price);
        $date_end = e($request->date_end);
        // url
        $urlExists = true;
        $i = 1;
        while ($urlExists) {
            $url = Str::slug($title . '-' . Str::random(8), '-');
            $checkUrlExists = Offer::where('url', $url)->count();
            $i++;
            if ($checkUrlExists == 0) {
                $urlExists = false;
            }
        }
        // imgs
        $imgNames = [];
        if ($imgs = $request->file('img')) {
            $i = 1;
            foreach ($imgs as $img) {
                $name = time() . '-' . $i . '.' . $img->getClientOriginalExtension();
                $img->move(storage_path('app/public/images/offers'), $name);
                array_push($imgNames, $name);
                $i++;
            }
        }
        // store
        Offer::create([
            'user_id' => auth()->user()->id,
            'title' => $title,
            'description' => $description,
            'url' => $url,
            'min_price' => $min_price,
            'date_end' => $date_end,
            'img' => serialize($imgNames)
        ]);
        // redirect
        Session::flash('success', 'Success!');
        return redirect('/offers')->withInput()->withErrors($validator);
    }
0 likes
3 replies
Nakov's avatar

Before the Offer::create if you can try and dump what does your imgNames contains?

using dd($imgNames) and make sure that each of the items of the array is just the name of the image and not an instance of UploadedFile, as that's where it seems to go wrong.

Snapey's avatar
Snapey
Best Answer
Level 122

You are trying to redirect with input.

This is not possible since your input contains files.

Redirects are always a GET request so anything that you might pass to that route would need to be converted to url query string parameters.

1 like
V1MPAR's avatar

@snapey Thats it! I change my redirect with input (and little validation code)

public function store(Request $request)
    {
        // validation
        $validator = Validator::make($request->all(), [
            'title' => 'required|min:4|max:128',
            'description' => 'required|min:16',
            'min_price' => 'required|numeric',
            'date_end' => 'required',
            'img' => 'required|max:10000'
        ]);
        // clear inputs
        $title = e($request->title);
        $description = e($request->description);
        $min_price = e($request->min_price);
        $date_end = e($request->date_end);
        // redirect if fails
        if ($validator->fails()) {
            return redirect('/offers/create')->withInput(
                [
                    'title' => $request->title, 
                    'description' => $request->description, 
                    'min_price' => $request->min_price, 
                    'date_end' => $request->date_end
                    ]
                )->withErrors($validator);
        }
        // url
        $urlExists = true;
        $i = 1;
        while ($urlExists) {
            $url = Str::slug($title . '-' . Str::random(8), '-');
            $checkUrlExists = Offer::where('url', $url)->count();
            $i++;
            if ($checkUrlExists == 0) {
                $urlExists = false;
            }
        }
        // imgs
        $imgNames = [];
        if ($imgs = $request->file('img')) {
            $i = 1;
            foreach ($imgs as $img) {
                $name = time() . '-' . $i . '.' . $img->getClientOriginalExtension();
                $img->move(storage_path('app/public/images/offers'), $name);
                array_push($imgNames, $name);
                $i++;
            }
        }
        // store
        Offer::create([
            'user_id' => auth()->user()->id,
            'title' => $title,
            'description' => $description,
            'url' => $url,
            'min_price' => $min_price,
            'date_end' => $date_end,
            'img' => serialize($imgNames)
        ]);
        // redirect
        return redirect('/offers')->with('success', 'Success!');
    }

and its working! Thanks all for help. Greetings all.

1 like

Please or to participate in this conversation.