我制作了一份更新表格,可以在其中编辑和更新我的租赁商店项目中的当前上传内容。 但是我一直遇到这个问题,其中页面和数据库中的图像都没有改变,但是当我检查存储/公共/图像时,只要我尝试再次尝试,它就会存储相同的当前图像。
但是我表单中的其余部分都可以正常工作,并且除了图像之外已经可以更改了。
这是在我的CarsController.php中
public function update(Request $request, $id)
{
$car = Car::find($id);
$car->car_brand = $request->car_brand;
$car->car_name = $request->car_name;
$car->description = $request->description;
$car->car_type_id = $request->car_type_id;
if ($request->hasFile('image_location')) {
Storage::disk('public')->delete($car->image_location); // remove the old file.
$path = $request->image_location->store('images', 'public'); // save the new image.
$car->image_location = $path;
}
$car->save();
$request->session()->flash('message', 'The item has been updated.');
return redirect('/selections');
}
这是表格
<div class="form-group">
<label>Image</label>
<input type="file" class="form-control" name="image_location" required>
</div>
我不知道可能有什么问题。任何人请赐教。提前致谢。无论如何,迁移的列名称是image_location
这也是在我的store()
public function store(Request $request)
{
$car = new car;
$car->car_brand = $request->car_brand;
$car->car_name = $request->car_name;
$car->description = $request->description;
$car->car_type_id = $request->car_type_id;
$path = $request->image_location->store('images', 'public');
$car->image_location = $path;
$car->availability = $request->availability;
$car->save();
$request->session()->flash('message', 'The item has been added.');
return redirect('/selections');
}
答案 0 :(得分:0)
恐怕您的代码对我来说是完美的。
您可以尝试添加一些日志行,看看问题出在哪里:
use Illuminate\Support\Facades\Log;
if ($request->hasFile('image_location')) {
Log::debug('File has been found in request.');
// Check if the old car image exists.
Log::debug($car->image_location . (Storage::disk('public')
->exists($car->image_location)? ' exists': ' does not exist'));
Storage::disk('public')->delete($car->image_location); // Remove the old file.
// Check if the old car image (if it existed) has been deleted.
Log::debug($car->image_location . (Storage::disk('public')
->exists($car->image_location)? ' exists': 'does not exist'));
$path = $request->image_location->store('images', 'public'); // save the new image.
// Check if the new file has been saved correctly.
Log::debug($path . (Storage::disk('public')
->exists($path)? ' exists': 'does not exist'));
$car->image_location = $path;
}
上传新图像后,我的storage/logs/laravel.log
显示:
[2019-12-28 10:18:03] local.DEBUG: File has been found in request. [2019-12-28 10:18:03] local.DEBUG: images/WPMkdG56f4YXsd3IUJ3Ar1DbXF0QmtDz9bs0lKoI.gif exists [2019-12-28 10:18:03] local.DEBUG: images/WPMkdG56f4YXsd3IUJ3Ar1DbXF0QmtDz9bs0lKoI.gif does not exist [2019-12-28 10:18:03] local.DEBUG: images/s9RtTdFSv0Is8AgxDWX5c09zG8SyU987RVeC0di5.gif exists