Yahya Essam
Yahya Essam

Reputation: 1912

Call to a member function hashName() on array Laravel 5.4

I'm trying to upload multiple images in same input and same row in the database same way worked with me with the single image upload but I got errors when I try it with Multiple images

here's my code :
Controller

$files = $request->file('file');
if(!empty($files)) :
    foreach($files as $file) :
      $name = time().$file->getClientOriginalName();
      Storage::putfile('public/images', $request->file('file'));
      $file->move('images/client/preview', $name);
      $car->file = $name;
    endforeach;
endif;

HTML

 <div class="form-group {{ $errors->has('file') ? ' has-error' : '' }} ">
  <input class="form-control" type="file" id="files" name="file[]" value="{{ old('file')}}" multiple />
  <output id="list"></output>
  @if($errors->has('file'))
  <div class="alert alert-danger alert-dismissable">
    <i class="fa fa-info"></i>
    <b>Alert!</b> {{ $errors->first('file') }}
  </div>
  @endif
</div>


Here's The error :

I got this error

Upvotes: 1

Views: 8836

Answers (1)

Uberswe
Uberswe

Reputation: 1058

Sorry I miss-read your question initially, don't pass request as the second argument of Storage::putfile() instead use the variable from your loop

$files = $request->file('file');
if(!empty($files)) :
    foreach($files as $file) :
      $name = time().$file->getClientOriginalName();
      Storage::putfile('public/images', $file);
      $file->move('images/client/preview', $name);
      $car->file = $name;
    endforeach;
endif;

The reason for the error was because the second argurment of Storage::putfile() was an array.

Upvotes: 4

Related Questions