Using Regular PHP Tags
The first way is to use regular starting and closing PHP tags like traditionally how it's written.
# views/partials/navbar.blade.php <?php $navLinks = [ 'Home' => route('pages.home'), 'Contact' => route('pages.contact'), 'About' => route('pages.about'), ]; ?> <div> @foreach($navLinks as $name => $link) <a href="{{ $link }}">{{ $name }}</a> @endforeach </div>
Using Blade Directive
The second way is to use PHP directive and it starts with @php and ends with @endphp
# views/partials/navbar.blade.php @php $navLinks = [ 'Home' => route('pages.home'), 'Contact' => route('pages.contact'), 'About' => route('pages.about'), ]; @endphp <div> @foreach($navLinks as $name => $link) <a href="{{ $link }}">{{ $name }}</a> @endforeach </div>
Leave a reply