Reputation: 421
How can i get a count of the number of arrays of the attached screenshot. In this scenario the number i want is 2
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
<form action="{{ route('employee.subjob.update',$job->id) }}" method="post"><br>
@csrf
@method('PUT')
@foreach ($subJobs as $subJob)
<input type="hidden" name="id[]" value="{{$subJob->id}}">
<tr>
<td><input type="text" name="name[]" value="{{$subJob->name}}"></td>
<td><input type="text" name="size[]" value="{{$subJob->size}}"></td>
<td>
<textarea name="medium[]" rows="3" cols="20">{{$subJob->medium}}</textarea>
</td>
<td>
<textarea name="feature[]" rows="3" cols="20">{{$subJob->feature}}</textarea>
</td>
<td><input type="text" name="qty[]" value="{{$subJob->qty}}"></td>
<td><input type="text" name="price[]" value="{{$subJob->price}}"></td>
<td><input type="text" name="total[]" value="{{$subJob->total}}" disabled></td>
</tr>
@endforeach
<button type="submit" class="btn btn-primary"> Update Job No {{$job->id}}</button>
</form>
Upvotes: 1
Views: 41070
Reputation: 14241
The thing is the way how you are sending the data to your backend. As it seems, your are grouping by attribute (an array of id
s then an array of name
s and so on) instead than by "class" (an array of elements that contains all the attributes in it: id
, name
, ...).
So, you could solve this issue in two ways.
For the first way, there are several guides/tutorials that can help you with. So, let's proceed with the second one.
public function update(Request $request, $jobId)
{
/** This should output 2 */
$size = count(collect($request)->get('id'));
// Try: 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();
}
}
Upvotes: 3
Reputation: 1016
Straight from your question "How can I get a count of the number of arrays of the attached screenshot"
here is how you do it, according to your question
$arrayCount = 0;
foreach ($request->request as $req)
{
// this will check for array and check the array has 2 elements
if (is_array($req) && is_array($req) == 2)
{
// your logic here
}
}
What I'm doing here that, I'm getting the request property of the request object. This allows me to loop through each property in the request object. Then I'm simply checking whether the property is an array or not. But here I only showed you how to get into the array. once you are in the array on the loop you can do whatever you want it
Upvotes: -1