laravel-5.8:为foreach()提供的参数无效

时间:2019-08-31 06:41:26

标签: laravel eloquent laravel-5.8

您好,我制作了一个表格以在Laravel中上传图片,但是当我打开表格时它显示错误:为foreach()提供了无效的参数

刀片文件:

    public function alternateimages(Request $request)
    {
        $altimgs = new Product;
        $altimgs->product_id = $request->product_id;
        $files = $request->file('product_alt_img');
        foreach ($files as $file) {
          $images = $file->getClientOriginalName();
          $file->move(public_path('images/backend_images/product_images'), $images);
          $altimgs->product_alt_img = $images;
        }
        $altimgs->save();

        return redirect()->back()->with('flash_message_success', 'Product Images has been added successfully');   
    }

功能:

FORMAT(yourTable[yourDateColumn],"MMMM")

1 个答案:

答案 0 :(得分:0)

如果要上传单个文件,则可能必须将product_alt_img[]更改为product_alt_img

<input type="file" name="product_alt_img" class="form-control mb-2 btn btn-sm btn-outline-primary mr-1 @error('product_image') is-invalid @enderror" value="{{ old('product_image') }}" id="" placeholder="">

否则,如果希望上传多个文件,则可能必须修改控制器方法。

 public function alternateimages(Request $request)
{
    $altimgs = new Product;
    $altimgs->product_id = $request->product_id;

    //fetch an array of product_alt_img
    $files = $request->product_alt_img;

    //go over each Illuminate\Http\UploadedFile instance
    foreach($files as $file)
    {
        $image = $file->getClientOriginalName();

        ....
    }

    ....  
}

NB:一种更有说服力的解决方案是使用一个单独的表来存储产品图像