简体   繁体   中英

Form laravel to database : Array to string conversion

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

}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM