Array Count - Laravel Request {#42 ▼
#json: null
#convertedFiles: null
#userResolver: Closure {#221 ▶}
#routeResolver: Closure {#224 ▶}
+attributes: ParameterBag {#44 ▶}
+request: ParameterBag {#43 ▼
#parameters: array:9 [▼
"_token" => "5SakQCnIBEL0SrBuKmH5QZveWJlsdPv8jltFfLl1"
"_method" => "PUT"
"id" => array:3 [▶]
"name" => array:3 [▶]
"size" => array:3 [▶]
"medium" => array:3 [▶]
"feature" => array:3 [▶]
"qty" => array:3 [▶]
"price" => array:3 [▶]
]
I need to get the array count as 3
public function update(Request $request, $jobId)
{
//dd($request);
// cast into a collection use the collect() helper
$collection = collect($request);
for ($i=0; $i < $collection->count(); $i++) {
//dd(count($request));
$subJob = SubJob::Find($request->get('id')[$i]);
$subJob->job_id = $jobId;
$subJob->name = $request->get('name')[$i];
$subJob->size = $request->get('size')[$i];
$subJob->medium = $request->get('medium')[$i];
$subJob->feature = $request->get('feature')[$i];
$subJob->qty = $request->get('qty')[$i];
$subJob->price = $request->get('price')[$i];
$subJob->total = $request->get('qty')[$i] * $request->get('price')[$i];
$subJob->save();
}
$collection->count() gives the value 9
Is there any other way of running the loop until the end? For example in blade we have $loop->last but here , i am working at the Controller level
Maybe get the request parameters rather than the entire Request instance:
$collection = collect($request->all());
Having said that I don't know why you are creating a collection when instead you could iterate of the array:
for ($i=0; $i < count($request->all()); $i++) {
// ...
}
@tykus
$collection = collect($request->all());
dd($collection->count());
and
$count = count($request->all());
dd($count);
Both gives the output 9
Ok, sorry I see now what you were trying to do... you need to transpose the Request parameters to get three distinct "forms". There is a handy Spatie package that defines a number of additional Collection macros , one of them is transpose() which you can use to transpose the request as required:
public function update(Request $request, $jobId)
{
collect($request->all())->transpose()->each(function ($subJobForm) use ($jobId) {
$subJob = SubJob::find($subJobForm['id']);
$subJob->job_id = $jobId;
$subJob->name = $subJobForm['name'];
$subJob->size = $subJobForm['size'];
$subJob->medium = $subJobForm['medium'];
$subJob->feature = $subJobForm['feature'];
$subJob->qty = $subJobForm['qty'];
$subJob->price = $subJobForm['price'];
$subJob->total = $subJobForm['qty'] * $subJobForm['price'];
$subJob->save();
});
}
The result of collect($request->all())->transpose() will be an array of length 3
@tykus
The below mentioned worked
$size = count(collect($request)->get('id'));
//dd($size);
for ($i = 0; $i < $size; $i++){
$subJob = SubJob::Find($request->get('id')[$i]);
$subJob->job_id = $jobId;
$subJob->name = $request->get('name')[$i];
$subJob->size = $request->get('size')[$i];
$subJob->medium = $request->get('medium')[$i];
$subJob->feature = $request->get('feature')[$i];
$subJob->qty = $request->get('qty')[$i];
$subJob->price = $request->get('price')[$i];
$subJob->total = $request->get('qty')[$i] * $request->get('price')[$i];
$subJob->save();
}
@zester This should do the same, with a bit less code, and a bit more clean
foreach ($request->id as $key => $size) {
$subJob = SubJob::Find($size);
$subJob->job_id = $jobId;
$subJob->name = $request->name[$key];
$subJob->size = $size;
$subJob->medium = $request->medium[$key];
$subJob->feature = $request->feature[$key];
$subJob->qty = $request->qty[$key];
$subJob->price = $request->price[$key];
$subJob->total = $subjob->qty * $subjob->price;
$subJob->save();
}
BTW, there's no sense in storing total since you can determine that at any time by qty*price, which you are already storing. So it's basically duplicate data that's unnecessary since it can be calculated at any time.
Please sign in or create an account to participate in this conversation.