How to looping foreach in laravel?

Member

by gilbert , in category: PHP Frameworks , 5 months ago

How to looping foreach in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 5 months ago

@gilbert 

To loop through a collection in Laravel using foreach, you can do the following:

  1. Retrieve the collection you want to loop through in your controller method:
1
$items = Item::all();


  1. Pass the collection to your view:
1
return view('your_view', compact('items'));


  1. In your Blade file, use the foreach loop to iterate over the collection:
1
2
3
@foreach($items as $item)
    {{ $item->name }}
@endforeach


This will loop through each item in the collection and print out the item's name.