1

enter image description here

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>
2
  • Have you tried first()? laravel.com/docs/5.6/collections#method-first. In addition, your question is not clear as to what exactly you're trying to achieve Commented Aug 4, 2018 at 13:58
  • @OluwatobiSamuelOmisakin i need to update my database with the inputs i get from these arrays. first() didn't work, i think i need to go through the Laravel - Collections documentation Commented Aug 4, 2018 at 14:11

3 Answers 3

3

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 ids then an array of names 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.

  1. Improving the way you are collecting the data in your frontend to send it to your back (recommended)
  2. Handle the data in the back as it is right now.

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your valuable answer. It worked :) If it is possible please let me know - if there are any links that you could suggest for me to go through on sending data from the frontend to the backend. I have updated the data collection part as well. I am collecting the data through a foreach loop therefore had to store them through arrays. Is there a better way to do this?
@zaster i'm afk right now, so I'm not 100% sure this would work, but try this: <input type="text" name="subJobs[]['name']" ...> and similar for every element, then dd($request->get('subJobs') or your whole dd($request) to see what is being sent. Please, tell me later if this worked.
yes that method seems much cleaner. Do you think that what you mentioned last has a performance improvement than the way i have approached?
Not about performance, because this are very simple operations for pc nowadays. I was more on the code styling /best practices side. For example, to count that it would be so much simpler: count($request->get('subJobs') does the trick. Besides, if another developer is working on your code, it would be much more readable when debugging.
1

Maybe try count($collection->items)

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.