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

Posted

技术标签:

【中文标题】使用 Laravel 8 将图像上传到存储和数据库时出现问题【英文标题】:problem when upload image to the storage and database with Laravel 8 【发布时间】:2022-01-03 11:03:48 【问题描述】:

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

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块内的错误消息.....当我忽略图像时,问题消失并且数据插入成功.....所以我确定问题来自插入图像>>>> >>提前致谢

【问题讨论】:

在表单标签处添加multipart/form-data 【参考方案1】:

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

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

【讨论】:

以上是关于使用 Laravel 8 将图像上传到存储和数据库时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

上传图片到 Laravel-Lumen API

无法使用 JQuery AJAX 将图像 + 文本数据上传到 Laravel

Laravel 8,如何一次上传多张图片

Laravel - 将文件上传到数据库

laravel rest api将文件上传到服务器

共享主机中的 Laravel 存储链接