0
votes

I am newbie in Laravel so i really need some help. I want to ask when I commented the part 'photo' => required why if I update without entering the photo it shows some error like call to a member function getClientOriginalName() on null. So the real question is I want to update without entering photo and it should still to be updated.

This is my code in Controller to upload photo

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

This is the code on my model

<?php

namespace App\Models;

use Eloquent as Model; use Illuminate\Database\Eloquent\SoftDeletes;

class Banner extends Model { use 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
Input::file('photo') is not an object - Scuzzy
So when the update without entering the photo will appear 'no image found' photo like this imgur.com/a/qzDVc - Sahat Riyanto
@Scuzzy hello thanks for response, so what should i do for this problem? - Sahat Riyanto
validate your input if the error goes away when you do choose a file. Your form screenshot has "no file chosen". Which would explain why Input::file('photo') is null - Scuzzy
@Scuzzy can you make the code because I'm really newbie in laravel: DD - Sahat Riyanto

3 Answers

1
votes
$validator = Validator::make(
                    $request->all(),
                    array(
                        'photo' => 'required',
                    ),
                    array(
                        'photo' => 'Please choose file',
                    )
                );

If Photo is not mandatory directly use this

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

Hope this will help.. let me know if any issue..Thank you

your update function would look like

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

The simplest validation required would be to test if Input::hasFile('photo'), this should be placed before you call 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

0
votes

You should check bellow code.

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

Before work with it.