Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

problem when upload image to the storage and database with Laravel 8

this is my code in controller for insert data to database:

public function store(Request $request)
    {

        $this->validate($request, [
            'name' => ['required', 'max:255'],
            'info' => ['required'],
            'price' => ['required', 'max:255'],
            'image' => ['required'],
        ]);

        $ServiceData = [
            'name' => $request->name,
            'info' => $request->info,
            'price' => $request->price,
        ];
        //store
        try {
            if ($request->hasFile('image')) {
                $dest_path = 'public/images/services';
                $image = $request->file('image');
                // $imageName = time().'.'.$request->image->extension();
                // $request->image->move(public_path('images'), $imageName);
                $image_name = time() . '.' . $image->getClientOriginalName();
                $image->storeAs($dest_path, $image_name);

            }
            Service::create([
                'name' => $request->name,
                'info' => $request->info,
                'price' => $request->price,
                'image' => $image_name,
                'category_id' => $request->category,
                'user_id' => auth()->id(),
            ])->save();
          //  $users = User::all();
            //send notification to all customers email.....
            // Notification::send($users, new servicesNotifications($ServiceData));
        } catch (\Exception $e) {
            return redirect()->back()->with('status', 'you cannot insert the Service');
        }

        return redirect()->route('services.index')->with('status', 'Service inserted successfully');
    }

and here the view for insert data:

<form action="{{route('services.store')}}" method="POST">
                @csrf
                <div class="mt-4">
                    <div>
                        <label class="block" for="Name">Name</label>
                        <input name="name" type="text" placeholder="Name"
                               class="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600">
                        @error('name') <small class="text-red-700">{{$message}}</small> @enderror
                    </div>

                    <div class="mt-4">
                        <div>
                            <label class="block" for="details">Details</label>
                            <input name="info" type="text" placeholder="Details"
                                   class="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600">
                            @error('details') <small class="text-red-700">{{$message}}</small> @enderror
                        </div>

                        <div class="mt-4">
                            <div>
                                <label class="block" for="City">Image</label>
                                <input name="image" type="file" placeholder="File"
                                       class="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600">
                                @error('image') <small class="text-red-700">{{$message}}</small> @enderror
                            </div>

                            <div class="mt-4">
                                <label class="block" for="price">Price</label>
                                <input name="price" type="text" placeholder="Price"
                                       class="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600">
                                @error('price') <small class="text-red-700">{{$message}}</small> @enderror
                            </div>

                            <div class="mt-4">
                                <label>
                                    <select name="category" class="w-full px-4 py-2 mt-2 border rounded-md focus:outline-none focus:ring-1 focus:ring-blue-600">
                                        @forelse($categories as $category)
                                            <option value="{{$category->id}}">{{$category->name}}</option>
                                        @empty
                                            <option value=""></option>
                                        @endforelse
                                    </select>
                                </label>

                                @error('categories') <small class="text-red-700">{{$message}}</small> @enderror
                            </div>

                            <div class="flex">
                                <button type="submit" class="w-full px-6 py-2 mt-4 text-white bg-blue-600 rounded-lg hover:bg-blue-900">Create Service</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

just show me the error message inside catch block….. when I ignore image, the problem disappear and data is inserted successfully….. so I am sure that the problem is from inserting image >>>>>> thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

add enctype="multipart/form-data" in your form

 <form action="" method="POST" enctype="multipart/form-data">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading