使用 Laravel 8 将图像上传到存储和数据库时出现问题

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

这是我在控制器中用于将数据插入数据库的代码:

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');
    }

这里是插入数据的视图:

<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>

给我看看 catch 块中的错误消息.....当我忽略图像时,问题消失并且数据插入成功.....所以我确定问题出在插入图像 >> >>>> 提前致谢

在您的表单中添加 enctype="multipart/form-data"

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