如果为null,如何检查倍数fotos

时间:2016-05-16 21:51:44

标签: php laravel

晚安!

我正在为Laravel5创建多个文件的验证。我在PHP中有这个代码,如果我发送图像

它可以工作
  public function uploadFotos(UploadFotosRequest $request){
      $image = $request->file('photo');

      $identificador = \Request::input('id');
      $pi=PI::find($identificador );
        foreach($image as $file){
              $fotosPI= new PhotosPI();
              $fotosPI->ruta = 'images/punto_interes/'.$pi->id.'/'.$file->getClientOriginalName();
              $file->move('images/pi/'.$pi->id.'/', $file->getClientOriginalName());
              $fotosPI->creador_id=Auth::user()->id;
              $fotosPI->punto_interes_id=$puntoInteres->id;
              $fotosPI->save();
        } 

    }

请求验证

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UploadFotosRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'photo' => 'required'
        ];
    }
}

当我没有发送任何图像时,我收到以下错误,我需要控制照片是否收到图像。

Call to a member function getClientOriginalName() on a non-object

1 个答案:

答案 0 :(得分:1)

尝试使用array_filter删除错误值(投射后):

$image = array_filter($request->file('photo'));