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
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.
dispatch_item_qty is an array of one entry. Not a string.
$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
@ftiersch Is there a function that i could use at once?
Or should i try to do that in a longer way?
@foram
$request->dispatch_item_qty[3]
In my code
3 is generated dynamically
array_values($request->dispatch_item_qty)[0]
That would work :) though it's not really beautiful :D
@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])]);
You have double arrays in your attach...
$sub_job->dispatches()->attach($dispatch_id, ['qty'=> array_values($request->dispatch_item_qty)[0]]);
you should use
current($request->dispatch_item_qty)
current function will give you first value of array
@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
Okay, you should have said that.
Maybe we should start somewhere else:
What do you actually need?
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.
@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
Okay, but does EACH dispatch have multiple quantities? Or does each quantity correspond to one dispatch?
@ftiersch
Each dispatch has multiple quantities
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.
Fix your form so that the data is in a format that suits being processed in the controller
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' => ???????]);
}
foreach($sub_jobs as $sub_job) {
$sub_job->dispatches()->attach($dispatch_id, ['qty' => $request->dispatch_item_qty[$sub_job->id]]);
}
@Snapey
Spot On! :) Thank you very much
@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 sign in or create an account to participate in this conversation.