Home > Software design >  problem when upload image to the storage and database with Laravel 8
problem when upload image to the storage and database with Laravel 8

Time:11-25

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 hljs-number">700">{{$message}}</small> @enderror
                            </div>

                            <div hljs-string">">
                                <button type="submit" hljs-number">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

CodePudding user response:

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

 <form action="" method="POST" enctype="multipart/form-data">
  • Related