Laravel8 - View loaded by @include in loop always keeps a value of the first loop

When I do things like this:

@foreach($items as $item)
  @include('loaded', ['item' => $item])
  {{ $item->name }}

$item in loaded.blade.php always has the same value, meaning the value of the first $item in loop.

Is this normal? If so, how can I avoid it. If that is not normal, what might be causing this problem?

1 answer

  • answered 2021-04-08 04:34 BABAK ASHRAFI

    You can use @each() method for this porpuse as described here in laravel doc

    @each('loaded', $items, 'item')

    Where loaded.blade.php is as follows

      {{ $item->name }}