代码之家  ›  专栏  ›  技术社区  ›  Sahat Riyanto

php-在null上调用成员函数getClientOriginalName(),并删除警报laravel

  •  0
  • Sahat Riyanto  · 技术社区  · 7 年前

    我是拉雷维尔的新手,所以我真的需要一些帮助。我想问一下什么时候 我评论了部分“照片”=>为什么在不输入照片的情况下更新时会显示一些错误,例如在null上调用成员函数getClientOriginalName()。所以,真正的问题是,我想在不输入照片的情况下进行更新,它应该仍有待更新。

    这是我在控制器中上传照片的代码

        public function update($id, UpdateBannerRequest $request)
    {
        $input = $request->all();
        //get original file name
        $filename = Input::file('photo')->getClientOriginalName();
        $input['photo'] =  $filename;
        Input::file('photo')->move($this->path, $filename);
        $banner = $this->BannerRepository->findWithoutFail($id);
    
        if (empty($banner)) {
            Flash::error('Banner not found');
    
            return redirect(route('banner.index'));
        }
    
        $banner = $this->BannerRepository->update($input, $id);
    
        Flash::success('Banner updated successfully.');
    
        return redirect(route('banner.index'));
    }
    

    这是我模型上的代码

    <?php
    

    命名空间应用程序\模型;

    使用Illumb\Database\Elount\SoftDeletes;

    类横幅扩展模型 { 使用软删除;

    public $table = 'banners';
    
    
    protected $dates = ['deleted_at'];
    
    
    public $fillable = [
        'title',
        'description',
        'photo',        
        'status'
    ];
    
    protected $casts = [
        'title' => 'string',
        'description' => 'string',
        'photo' => 'string',       
        'status' => 'integer'
    ];
    
    
    
    public static $rules = [
        'title' => 'required',
        'description' => 'required',
        //'photo' => 'required',
        'status' => 'required'
    ];
    

    This is the view and This is the error

    3 回复  |  直到 7 年前
        1
  •  1
  •   Rohit shah    7 年前
    $validator = Validator::make(
                        $request->all(),
                        array(
                            'photo' => 'required',
                        ),
                        array(
                            'photo' => 'Please choose file',
                        )
                    );
    

    如果照片不是强制性的,请直接使用

    if(!empty($request->photo)){
       //do something
    }
    else{
      Flash::error('Banner not provided');
      return redirect(route('banner.index'));
    }
    

    你的更新功能看起来像

         public function update($id, UpdateBannerRequest $request)
            {
                $input = $request->all();
                $banner = $this->BannerRepository->findWithoutFail($id);
                if(!empty($request->photo)){
                   //do something for saving the name of file in database and other value respectively using
        //         $filename = Input::file('photo')->getClientOriginalName();
        //         $banner->photo = $filename;
                }
                else{
                   Flash::error('Banner not provided');
                   return redirect(route('banner.index'));
                }
                $banner->save();
                Flash::success('Banner updated successfully.');
                return redirect(route('banner.index'));
            }
    
        2
  •  0
  •   Scuzzy    7 年前

    Input::hasFile('photo') Input::file('photo')->getClientOriginalName()

    if( Input::hasFile('photo') == false )
    {
      Flash::error('Banner not provided');
      return redirect(route('banner.index'));
    }
    

    https://laravel.com/docs/4.2/requests#files

        3
  •  0
  •   Jalpesh Patel Kiran S youtube channel    7 年前

    您应该检查以下代码。

    if(isset(Input::file('photo'))