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

时间:2021-04-22 17:10:16

标签: php sql laravel image upload

我想一次通过 Laravel 8 中的一个上传多个图像到我的 SQL 数据库,但我无法做到。我只上传了一个,但当我尝试上传更多时,我会失败。

我的数据库

图像

<头>
id 名词 id_coche
01 名称.jpg 0004
... ... ...

我的代码

带形状的刀片


                   @foreach ($Vehiculo as $obj) /*this is to take the Car ID*/
                            <form method="POST" action="{{ route('añadirImagen')}}" enctype="multipart/form-data" >
                                @csrf
                                <div class="form-row">
                                    <div class="form-group col-md-3">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text">ID</span>
                                            </div>
                                            <input type="text" class="form-control" name="id_coche" value="{{$obj->id}}" style="background: white" required readonly>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="file" class="form-control" name="imagen" required multiple/>
                                    </div>
                                    <div class="form-group col-md-3">
                                        <button type="submit" class="btn btn-success">AÑADIR IMAGENES</button>
                                    </div>                           
                                </div>
                            </form>
                        @endforeach

控制器

“只上传一张图片”


    public function añadirImagen(Request $request){

        $validated = $request->validate([
            'id_coche' => 'required',
            'nombre.*' => 'mimes:image'
        ]);

        $data = $request->input();

        $id_coche = $data['id_coche'];

        $Imagenes= new Imagenes;

        $Imagenes->id_coche = $id_coche;

        if($request->file("imagen")!=null){
            $nombre = $request->file('imagen');
                $nombreFoto = $nombre->getClientOriginalName();
                $nombre->move('img/Coches/', $nombreFoto);
                $Imagenes->nombre = $nombreFoto;
        }

        $Imagenes->save();

        return redirect()->back()->with('error','Se han añadido las imagenes correctamente.');
    }
}

“我尝试上传多个”

public function añadirImagen(Request $request){

        $validated = $request->validate([
            'id_coche' => 'required',
            'imagen.*' => 'mimes:image'
        ]);

        $data = $request->input();

        $id_coche = $data['id_coche'];

        $Imagenes= new Imagenes;

        $Imagenes->id_coche = $id_coche;

        if($request->hasfile("imagen")){
            $nombre_img = $request->file('imagen');
            foreach($nombre_img as $nombre) {
                $nombreFoto = $nombre->getClientOriginalName();
                $nombre->move('img/Coches/', $nombreFoto);
                
                $Imagenes->nombre = $nombreFoto;
            }
        }

        $Imagenes->save();

这样做时,它会在数据库中添加一行,使用正确的 id_coche,Auto_Increment 处理好 ID,但名称仍然为 NULL。

谢谢。

1 个答案:

答案 0 :(得分:0)

您目前拥有:

<input type="file" class="form-control" name="imagen" required multiple/>

它必须是:

<input type="file" class="form-control" name="imagen[]" required/>

不需要多个属性。 然后你可以在你的控制器中做:

if($request->hasfile('imagen')) {
     foreach($request->file('imagen') as $file)
        {
            ...
        }
}