1

Hello I am trying to integrate some of my products with another table I receive the names of the products by a table and by them I send the images through the form file. I'm getting this error

ErrorException

Array to string conversion

by dump the data arrives correctly

dd

array:3 [▼
  "id" => "543865555"
  "pedidoitem" => array:3 [▼
    0 => "Pulseira Tyvek Reta Personalizada - Branco"
    1 => "Pulseira Tyvek Reta Personalizada - Prata"
    2 => "Pulseira Tyvek Reta Personalizada - Azul Claro"
  ]
  "pedidoanexo" => array:3 [▼
    0 => "image/20191226.futwin.jpg"
    1 => "image/20191226.pp.jpg"
    2 => "image/20191226.MAPA-DO-TUR-2017.jpg"
  ]
]

View

@foreach($users as $user)
    <option value="{{$user->name}}">{{$user->name}}</option>

  @endforeach


    </select>

  </div>




  @foreach($itens as $item)
    <div class="form-group">
  <select class="form-control" name="products[]">
    <option value="{{$item->pedidoitem}}">{{$item->pedidoitem}}</option>
    </select>
</div>
    <div class="form-group">
  {!! Form::file('pedidoanexo[]') !!}
  </div>
    @endforeach

Controller

if ($request->hasFile('pedidoanexo')) {
$store_file = [];
$files = $request->file('pedidoanexo');
foreach ($files as $file) {
  $destination_path = 'image/';
  $profileImage = date("Ymd").".".$file->getClientOriginalName();
  $file->move($destination_path, $profileImage);
  $pedidoanexo[] = $destination_path . $profileImage;
}
    $store_file = [

        'id' => $id,
        'pedidoitem' => $_POST['products'],
        'pedidoanexo' => $pedidoanexo

    ];

DB::table('images')->insert($store_file);

}
3
  • you need a foreach for this <option value="{{$item->pedidoitem}}">{{$item->pedidoitem}}</option> Commented Dec 26, 2019 at 23:46
  • get property of non object :\ Commented Dec 27, 2019 at 1:22
  • try using array syntax @foreach($item['pedidoitem'] as $pedidoitem) <option value="{{$pedidoitem}}">{{$pedidoitem}}</option> @endforeach Commented Dec 27, 2019 at 3:40

2 Answers 2

2

Check the structure of the array again.

@foreach($items['pedidoitem'] as $item)
    <div class="form-group">
        <select class="form-control" name="products[]">
            <option value="{{$item}}">{{$item}}</option>
        </select>
    </div>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

1

You have an array within the element of an array, so for example if you want to access "Pulseira Tyvek Reta Personalizada - Branco" you should go with:

$item->pedidoitem[0]

You can dd($item->pedidoitem) and you will see the following:

array:3 [▼
    0 => "Pulseira Tyvek Reta Personalizada - Branco"
    1 => "Pulseira Tyvek Reta Personalizada - Prata"
    2 => "Pulseira Tyvek Reta Personalizada - Azul Claro"
  ]

This means that you need to select an element from the resulting array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.