Call to a member function getClientOriginalName() on array laravel

14,988

Solution 1

I think you have a problem inside store1() method. You sould not re-declare $file.

if($file = $request->hasFile('input_img')) {

    $file = array();

    $fileName = $file->getClientOriginalName();

Instead get the file using the file() method:

if($request->hasFile('input_img')) {

    $file = $request->file('input_img');

    $fileName = $file->getClientOriginalName();

See more on the Laravel's requests documentation.

Solution 2

Read through your logic in the store1 method again... You are setting the $file variable to an empty array and then trying to call the getClientOriginalName() method on it:

$file = array();

$fileName = $file->getClientOriginalName();
Share:
14,988
blastme
Author by

blastme

Updated on June 04, 2022

Comments

  • blastme
    blastme almost 2 years

    I'm trying to post an image from a one to many relationship while also doing the CRUD (create part), but I am having some trouble doing it. I keep on getting this error,Call to a member function getClientOriginalName() on array, whenever I try to use associate to define the relationship together with user_info with user_image table. So what should I do?

    Here are my codes: createController:

    public function create1(){
    
        return view('create1');
    }
    
    public function store1(Request $request){
         $this->validate($request, [
            'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
    
     $user_info = Session::get('data');
          $UserImage = new UserImage($request->input()) ;
    
    
         if($file = $request->hasFile('input_img')) {
          $file = array();
    
    
    
            $fileName = $file->getClientOriginalName() ;
            $destinationPath = public_path().'/images' ;
            $file->move($destinationPath,$fileName);
            $UserImage->userImage = $fileName ;
            $UserImage = UserImage::create(['file' => $request->file('input_img')]);
            $UserImage->user_infos()->associate($user_info);
        }
    
        $UserImage->save() ;
    
        return redirect('/home');
    }
    

    HomeController(this is where I print out my information)

    public function getInfo($id) {
    
      $data = personal_info::where('id',$id)->get();
          $data3=UserImage::where('user_id',$id)->get();
     return view('test',compact('data','data3'));
    

    blade.php (how I show the image in view)

     @foreach ($data3 as $object9)
     <img width="100" height="100" src="{!! $object9->signature !!}">
        @endforeach
    

    UserImage model(in table I used binary format to store in DB)

        class UserImage extends Eloquent
        {
                protected $fillable = array('userImage','user_id');
            public function user_infos() {
                return $this->belongsTo('App\user_info', 'user_id', 'id');
            }
    
    class user_info extends Eloquent
    {
        protected $fillable = array('Email', 'Name');
        protected $table = user_infos';
        protected $primaryKey = 'id';
            public function UserImages() {
            return $this->hasOne('App\UserImage','user_id');
        }
    }
    

    create1.blade.php(this is how I upload the image)

         <form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
    
                            {{  csrf_field()  }}
    
    
    <div class="form-group">
        <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
                <div class="col-sm-9">
                <input data-preview="#preview" name="input_img" type="file" id="imageInput">
                <img class="col-sm-6" id="preview"  src="" ></img>
            </div>
        </div>
    
     <div class="form-group">
                <div class="col-md-6-offset-2">
                  <input type="submit" class="btn btn-primary" value="Save">
                </div>
              </div>
              </form>
    
    • Camilo
      Camilo over 6 years
      On which line of which file are you getting the error?
    • blastme
      blastme over 6 years
      I'm getting it on this line "$fileName = $file->getClientOriginalName() ;"
  • blastme
    blastme over 6 years
    Hi Camilo, tks for the reply, I am now getting this error instead, "Call to a member function getClientOriginalName() on boolean", any idea?
  • Camilo
    Camilo over 6 years
    Yes, you need to grab the uploaded file calling $request->input_img or $request->file('input_img'). Updating my answer.
  • blastme
    blastme over 6 years
    Hi, Camilo your answer worked !! tks a lot but suddenly it couldn't be saved into database
  • Camilo
    Camilo over 6 years
    That's because your trying to store the file itself into the database. A common practice it's to only store the file name.
  • blastme
    blastme over 6 years
    But I want to retrieve it and show it in my blade.php based on the user_id of the relationship between user_info and userImage table. Example, Jack (id =1 from user_info) must link back with Image(user_id=1 from userImage). Or is there another way instead of doing in the database itself
  • Camilo
    Camilo over 6 years
    I recommend you searching for that specific problem on the web or creating a new question for it. Please, if my response answered your question consider accepting it.
  • blastme
    blastme over 6 years
    Ok, tks a lot for your help :)