Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaster's avatar

Array to String Conversion Error

+request: ParameterBag {#43 ▼
    #parameters: array:6 [▼
      "_token" => "cQQv8KMaPeskjVq6GmspJz3SO4gh7PTe5yj0psrz"
      "dispatch_item_qty" => array:1 [▼
        3 => "5"
      ]

controller

foreach($sub_jobs as $sub_job)     
{                       
$sub_job->dispatches()->attach($dispatch_id, [array('qty'=> $request->dispatch_item_qty)]); 
}

As i understand, the value of dispatch_item_qty should be converted from a string to a numerical value

0 likes
21 replies
ftiersch's avatar

It's not a string, it's an array. That's your problem. You have to get the value "5" from your array, then it will work.

Snapey's avatar

dispatch_item_qty is an array of one entry. Not a string.

foram's avatar

$request->dispatch_item_qty is array and you have passed as value that's why it shows Array to string conversation error

use like that $request->dispatch_item_qty[3]

by this way you will get proper value and resolve your error

zaster's avatar

@ftiersch Is there a function that i could use at once?

Or should i try to do that in a longer way?

zaster's avatar

@foram

 $request->dispatch_item_qty[3]

In my code

3 is generated dynamically

ftiersch's avatar
array_values($request->dispatch_item_qty)[0]

That would work :) though it's not really beautiful :D

zaster's avatar

@ftiersch

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'  
$sub_job->dispatches()->attach($dispatch_id, [array('qty'=> array_values($request->dispatch_item_qty)[0])]);  
ftiersch's avatar

You have double arrays in your attach...

$sub_job->dispatches()->attach($dispatch_id, ['qty'=> array_values($request->dispatch_item_qty)[0]]);  
foram's avatar

you should use

current($request->dispatch_item_qty)

current function will give you first value of array

zaster's avatar

@foram

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'      

Please note that in my real requirement, there can be multiple values in the dispatch_item_qty array

ftiersch's avatar

Okay, you should have said that.

Maybe we should start somewhere else:

What do you actually need?

foram's avatar

implode(',',$request->dispatch_item_qty)

this will provide you comma separated value so you have all array value in string.

rest you have to mange it as per your DB relation tables.

zaster's avatar

@ftiersch @foram

The pivot table should be updated with this extra qty field and those quantities are inputted by a form and passed through an array (dispatch_item_qty)

I need to get the values from this array and insert in to the pivot table

Tried this

foreach($sub_jobs as $sub_job)
{
   $sub_job->dispatches()->attach($dispatch_id, ['qty' => implode(',',$request->dispatch_item_qty)]);
}

and received this error

SQLSTATE[01000]: Warning: 1265 Data truncated for column 'qty' at row 1

ftiersch's avatar

Okay, but does EACH dispatch have multiple quantities? Or does each quantity correspond to one dispatch?

ftiersch's avatar

Okay, that makes things clearer. In that case you'll have to decide how to save the data - depending on how many quantities there could be. The error above means that it was probably too much for the field you used for your "qty" column.

That should probably be a TEXT or JSON column then.

Snapey's avatar

Fix your form so that the data is in a format that suits being processed in the controller

zaster's avatar

form

{{-- <th>Dispatch Item Qty</th> --}}
<td class="col-md-2">                                          
  <input type="text" class="form-control" name="dispatch_item_qty[{{$sub_job->id}}]">
 </td>  

dd($request);

+request: ParameterBag {#43 ▼
    #parameters: array:6 [▼
      "_token" => "cQQv8KMaPeskjVq6GmspJz3SO4gh7PTe5yj0psrz"
      "dispatch_item_qty" => array:3 [▼
        3 => "5"
        4 => "10"
        5 => "100"
      ]

controller

foreach($sub_jobs as $sub_job)
{
$sub_job->dispatches()->attach($dispatch_id, ['qty' => ???????]);
}
Snapey's avatar
Snapey
Best Answer
Level 122
foreach($sub_jobs as $sub_job) {
    $sub_job->dispatches()->attach($dispatch_id, ['qty' => $request->dispatch_item_qty[$sub_job->id]]);
}
zaster's avatar

@ftiersch

I think i have mislead you with this

Each dispatch has multiple quantities

I am sorry about that

Each dispatch has multiple dispatch_items and each dispatch_item has one qty. The pivot table in my case represent a dispatch_item

Anyway i still have doubts with the logic and code etc... need to give some more thought to figure it out

Thank you very much for your support

Please or to participate in this conversation.