有人可以帮我在 laravel 中修复当我在数据库上添加图像时我得到这个 D:\youssfi\wamp mp\php5571.tmp 但我想要图像的原始名称

问题描述 投票:0回答:1

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

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 App\Repositories;

    use App\Vehicule;
    use App\Http\Requests\VehiculeRequest;

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

    }
php laravel
1个回答
0
投票

如果你想得到原来的名字,可以用这个方法

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

您可以查看此 UploadFile API 文档,了解有关可以使用哪种方法的更多信息,对于

array_merge
,您可以阅读此 文档

© www.soinside.com 2019 - 2024. All rights reserved.