当我在数据库上添加图像时,有人可以在 Laravel 中帮助我修复我得到了这个 D:\youssfi\wamp\t



这是我在控制器中的函数存储:

public function store(VehiculeRequest $request)
{
$image = $request->image_VH;
if($image->isValid()) {
$chemin = config('images.path');
$extension = $image->getClientOriginalExtension();
do {
$nom = str_random(10) . '.' . $extension;
} while(file_exists($chemin . '/' . $nom));
$image->move($chemin, $nom);
}
$inputs = array_merge($request->all($image));
$this->VHRepository->store($inputs);
return redirect(route('vehicules.index'));
}

这是我的类存储库:

<?php
namespace AppRepositories;
use AppVehicule;
use AppHttpRequestsVehiculeRequest;
class VHRepository
{
protected $Vehicule;
public function __construct(Vehicule $Vehicule)
{
$this->Vehicule = $Vehicule;
}
public function getPaginate($n)
{
return $this->Vehicule->with('user')
->orderBy('vehicules.created_at', 'desc')
->paginate($n);
}
public function store($inputs)
{
$this->Vehicule->create($inputs);
}
public function destroy($id_vehicules)
{
$this->Vehicule->findOrFail($id_vehicules)->delete();
}
}

如果要获取原始名称,可以使用此方法getClientOriginalName

因此,控制器中的方法应如下所示:

控制器

public function store(VehiculeRequest $request)
{
$image = $request->image_VH;
if($image->isValid()) {
$chemin = config('images.path');
do {
$nom = $image->getClientOriginalName();
} while(file_exists($chemin . '/' . $nom));
$image->move($chemin, $nom);
}
// because the $image variable contain uploadedFile object, you cannot use array_merge on it.
// if you want to save the original name in your database you have to change the $image variable with $nom
// and for the usage of array_merge you have to add the second parameter on it
$inputs = array_merge($request->all(), ['image_VH' => $nom]);
// and if you want to use the path also when save that image in your DB
$inputs = array_merge($request->all(), ['image_VH' => $chemin . '/' . $nom]);
// and for the last store it
$this->VHRepository->store($inputs);
return redirect(route('vehicules.index'));
}

您可以查看此上传文件 API 文档,了解有关可以使用的方法的更多信息,以及array_merge可以阅读此文档

最新更新