tomasosho's avatar

ErrorException Undefined variable: fileNameToStore

View

<form class="input-form" action="{{ route('slider.store') }}" method="POST">
                    {{ csrf_field() }}
                    <label class="control-label mt-3" for="example-input1-group2">New Slider</label>
                    <div class="row">
                        <div class="col-lg-4">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <label class="btn btn-danger" type="button">Upload Image</label>
                                </div>
                                <input type="file" class="form-control" name="image" accept="image/*">
                            </div>
                        </div>
                        <div class="col-md-4 mb-3 mb-md-0">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <label class="btn btn-info">Text</label>
                                </div>
                                <input type="text" name="text" class="form-control" placeholder="Enter your text here...">
                            </div>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-12">
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>
                    </div>
                    <!-- form-group -->
                </form>

Controller

public function store(Request $request)
    {
        // dd($request->input('image'));
        $data = request()->validate([
            'image' => 'required',
        ]);

        // Handle File Upload
        if($request->hasFile('image')){
            // Get filename with the extension
            $filenameWithExt = $request->file('image')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('image')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('image')->storeAs('public/slider', $fileNameToStore);
        }
        

        $slider = new Slider;
        $slider->image = $fileNameToStore;
        $slider->text = $request->input('text');
        $slider->save();
        return back()->with('success', 'Saved Successfully');
    }
0 likes
3 replies
tomasosho's avatar

when i get to dd($request->input('image')); it returns my image name, but when i dd($request->hasFile('image')); it fails.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Add this to the form tag enctype="multipart/form-data"

Also make sure to set a default for the variable (if there is no file)

$fileNameToStore= ''
1 like
tomasosho's avatar

grateful, i just totally overlooked that 😭

Please or to participate in this conversation.