0

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

1
  • 2
    add multipart/form-data at the form tag Commented Nov 25, 2021 at 6:17

1 Answer 1

1

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

 <form action="" method="POST" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.