0

I am trying to display the distance for each of my drivers on my database in my view.

When I do die or dump for $pickupDistanceAndTime2 it returns the arrays below, so I need each of the drivers to have its own distance.

0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
return view('driverspane', [
    'drivers' => $driver,
    'rideDistancenTime' => $pickupDistanceAndTime2,
]);

In my view, I am looping but not getting the way I want it to appear.

@foreach ($drivers as $item)
    @foreach ($rideDistancenTime as $data)
        {{ $data[0] }}
    @endforeach
@endforeach

But the above code keeps repeating distance and time for each of the driver, but I want each of the driver to have its own distance and time only, please see the result I am getting below attached. enter image description here

The array for $driver returns

#items: array:3 [▼
    0 => App\Driver {#317 ▶}
    1 => App\Driver {#300 ▶}
    2 => App\Driver {#281 ▶}
  ]
4
  • How are $drivers data related to $rideDistancenTime? In theory the second one should have an index that put in relation its data with the driver, so the second foreach is like @foreach ($rideDistancenTime as[$item] $data) Commented Oct 10, 2022 at 11:03
  • Please update your question to expand the contents of the very first array that you ddied Commented Oct 10, 2022 at 11:03
  • ok let me do that now Commented Oct 10, 2022 at 11:08
  • edited please check Commented Oct 10, 2022 at 11:14

2 Answers 2

1

From what you did above, you are just repeating the same values for the first index of the array. You should not do a nested foreach statement.

Rather you could do it this way(using a for statement, provided that the arrays all have same number of keys:

 @for ($i=0; $i<count($drivers); $i++)
      
      {{$rideDistancenTime[$i][1]}}
      
@endfor

Now it should loop properly.

Sign up to request clarification or add additional context in comments.

7 Comments

let me check with this and get back please
I made an error. I have edited my answer.
am i loopin this in the view or controller?
In your view file.
Replace the foreachs you had previously.
|
0

You can use below code to achieve your goal:

$rideDistancenTime;

@foreach ($drivers as $key => $item)
    
      {{ $rideDistancenTime[$key] }}

@endforeach```

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.