MattB's avatar
Level 2

How to update images with array

I have a form that allows for the upload of multiple images alongside other inputs and that works fine. However, I cannot update the entries with multiple images as I get the error:

Array to string conversion

Here is the part of the form which handles the upload:

<div class="form-group {{$errors->has('photo') ? 'has-error' : ''}}">
      {!! Form::label('photo', 'Image 1:') !!}
      {!! Form::file('photo', null, ['class'=>'form-control'])!!}
      @if($errors->has('photo'))
        {{$errors->first('photo')}}
      @endif
    </div>
    <div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
      {!! Form::label('image', 'Image 2:') !!}
      {!! Form::file('image[]', null, ['class'=>'form-control'])!!}
      @if($errors->has('image'))
        {{$errors->first('image')}}
      @endif
    </div>
    <div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
      {!! Form::label('image', 'Image 3:') !!}
      {!! Form::file('image[]', null, ['class'=>'form-control'])!!}
      @if($errors->has('image'))
        {{$errors->first('image')}}
      @endif
    </div>
    <div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
      {!! Form::label('image', 'Image 4:') !!}
      {!! Form::file('image[]', null, ['class'=>'form-control'])!!}
      @if($errors->has('image'))
        {{$errors->first('image')}}
      @endif
    </div>

and the method:

public function update(GamesRequest $request, $id)
    {
          $gameSearch = Game::findOrFail($id);
          $photoSearch = Photo::findOrFail($id);
          $input = $request->all();
          if($file = $request->file('photo')){
            $name = $file->getClientOriginalName();
            $file->move('images/games/', $name);
            $input['photo'] = '/images/games/' . $name;
          }
            $input['title'] = $request->title;
            $input['price'] = $request->price;
            $input['score'] = $request->score;
            $input['description'] = $request->description;
            $input['category_id'] = $request->category_id;
            $input['promote'] = 0;
            $input['sold'] = $request->sold;
            $input['manual'] = $request->manual;
            $input['box'] = $request->box;
            $input['realphoto'] = $request->realphoto;
            $input['youtube'] = $request->youtube;
          $gameSearch->update($input);

        // Handle multiple file upload
        if($images = $request->file('image'))
        {
            foreach($images as $key => $image) {
                $name = $image->getClientOriginalName();
                if ($image->move('images/games/', $name)) {
                    // store image to database.
                    $input = $request->all();
                    $input->game_id = $input->id;
                    $input->image = '/images/games/' . $name;
                    $photoSearch->update($input);
                }
            }
        }
        return redirect('admin/games');
            }

When I upload just one photo that doesn't use the image array, it works fine

0 likes
9 replies
ftiersch's avatar
 $input = $request->all();
 $input->game_id = $input->id;
 $input->image = '/images/games/' . $name;

This part can't be correct since all() returns an array of values (most of which you don't need at that point I guess?) and you use it like an object in the next line :)

MattB's avatar
Level 2

I changed it to

$input = $request;

And also removed the line altogether but it didn't help

ftiersch's avatar

I'm really not sure what you are trying to achieve with that code.

 $gameSearch = Game::findOrFail($id);
 $photoSearch = Photo::findOrFail($id);

You are getting a Photo object with the same ID as your game (but you have multiple photos per game?!). Then in your foreach loop you update that single photo object over and over again so basically every change is lost despite the last one.

I'm not quite sure where your array to string conversion error is coming from but a lot in that method cannot work.

dd($request->all());

What does this show?

MattB's avatar
Level 2

yeah, I don't really know how to go about updating with multiple files. dd shows:

array:14 [▼
  "_method" => "PATCH"
  "_token" => "MaP0WnARbUaRqlC4DNILgmW9TyUw9Aqt3fh11ioi"
  "title" => "Quackshot"
  "youtube" => null
  "price" => "15"
  "score" => "9"
  "description" => "Changed photos"
  "category_id" => "2"
  "sold" => "0"
  "manual" => "0"
  "box" => "0"
  "realphoto" => "0"
  "photo" => UploadedFile {#325 ▶}
  "image" => array:1 [▶]
]
ftiersch's avatar

It's not about the updating itself... The updating is quite alright with a foreach loop. But I'm not sure if your database structure is correct and everything around it :) It's a complex topic so better approach it step by step with a little more planning.

Also, have you looked at packages for this? Like spatie/laravel-medialibrary. I have used that before and it makes life SO much easier!

MattB's avatar
Level 2

Ok on further inspection of the error it's actually this line causing the problem:

$gameSearch->update($input);

Because this:

$input = $request->all();

Is also grabbing the image array:

array:14 [▼
  "_method" => "PATCH"
  "_token" => "MaP0WnARbUaRqlC4DNILgmW9TyUw9Aqt3fh11ioi"
  "title" => "Quackshot"
  "youtube" => null
  "price" => "15"
  "score" => "9"
  "description" => "Changed photos"
  "category_id" => "2"
  "sold" => "0"
  "manual" => "0"
  "box" => "0"
  "realphoto" => "0"
  "photo" => UploadedFile {#325 ▶}
  "image" => array:1 [▶]
]

Is there a way to remove this from the initial $request->all() and have it just for use by the foreach loop, or is that not possible:

"image" => array:1 [▶]
ftiersch's avatar

Sure there is :)

You can use $request->except('image') to get everything but the image and $request->only('image') as the opposite :)

MattB's avatar
Level 2

Ok that sorts out the initial error but when I try to upload now, all I get is a 404 page. Checked db and nothing was updated so I don't think it's a routing issue on redirect

ftiersch's avatar
ftiersch
Best Answer
Level 28

That means your findOrFail() failed. So probably there is no Photo with the same ID as the game. That's what I meant earlier that you need to think more about the structure of it all.

Please or to participate in this conversation.