To pass links to a component in Laravel, you can make use of component slots. Here's how you can modify your code to achieve this:
- Modify your
index.blade.phpfile to include thex-breadcrumbcomponent and pass the$breadcrumbsvariable as a slot:
<x-app-layout>
<x-breadcrumb>
@php
$breadcrumbs = [
['name' => 'Home', 'url' => '/'],
['name' => 'Roles', 'url' => '/roles'],
['name' => 'Roles', 'url' => '/roles/test', 'current' => true],
];
@endphp
@foreach ($breadcrumbs as $breadcrumb)
<x-breadcrumb-item :breadcrumb="$breadcrumb" />
@endforeach
</x-breadcrumb>
</x-app-layout>
-
Create a new component called
Breadcrumbusing thephp artisan make:component Breadcrumbcommand. This will generate a new component class in theapp/View/Componentsdirectory. -
Open the
Breadcrumb.phpfile and modify it as follows:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Breadcrumb extends Component
{
public function render()
{
return view('components.breadcrumb');
}
}
- Create a new blade file called
breadcrumb.blade.phpin theresources/views/componentsdirectory and add the following code:
<nav class="flex" aria-label="Breadcrumb">
<ol class="inline-flex items-center space-x-1 md:space-x-3">
{{ $slot }}
</ol>
</nav>
-
Create a new component called
BreadcrumbItemusing thephp artisan make:component BreadcrumbItemcommand. This will generate a new component class in theapp/View/Componentsdirectory. -
Open the
BreadcrumbItem.phpfile and modify it as follows:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class BreadcrumbItem extends Component
{
public $breadcrumb;
public function __construct($breadcrumb)
{
$this->breadcrumb = $breadcrumb;
}
public function render()
{
return view('components.breadcrumb-item');
}
}
- Create a new blade file called
breadcrumb-item.blade.phpin theresources/views/componentsdirectory and add the following code:
@if($breadcrumb['current'])
<li aria-current="page">
<div class="flex items-center">
<svg class="w-3 h-3 text-gray-400 mx-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4"/>
</svg>
<span class="ml-1 text-sm font-medium text-gray-500 md:ml-2 dark:text-gray-400">{{ $breadcrumb['name'] }}</span>
</div>
</li>
@else
<li>
<div class="flex items-center">
<svg class="w-3 h-3 text-gray-400 mx-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4"/>
</svg>
<a href="{{ $breadcrumb['url'] }}" class="ml-1 text-sm font-medium text-gray-700 hover:text-blue-600 md:ml-2 dark:text-gray-400 dark:hover:text-white">{{ $breadcrumb['name'] }}</a>
</div>
</li>
@endif
Now, when you render the index.blade.php file, the $breadcrumbs variable will be passed to the x-breadcrumb component, which will then render each breadcrumb item using the x-breadcrumb-item component.