A route picker field that lets you select routes from a predefined collection.
The route field requires the name of the collection that should be used for this field. Additionally a title is required.
$form->route('route')
->collection('app')
->title('Picke a Url');
To be able to select routes in your route field you must first register a route collection in a service provider. The registered collection can then be used again and again in different places. The following example registers a collection with the name app.
use Ignite\Crud\Fields\Route;
Route::register('app', function($collection) {
// Define your route collection in here.
});
The selectable routes are configured in the closure using the route
method.
The first parameter is the title that represents the route followed by an
idendifier and a function that returns the actual route.
$collection->route('Home', 'home', function() {
return route('home');
});
Use the arrow function short cut:
$collection->route('Home', 'home', fn($locale) => route('home'));
You can group routes for example into model show routes.
$collection->group('Articles', function($group) {
$articles = Article::all();
foreach($articles as $article) {
$group->route($article->title, $article->id, function() use ($article) {
return route('articles.show', $article->id));
});
}
});
The closure in which the route is returned is given the current locale as parameter.
$collection->route('Home', 'home' function($locale) {
return route("{$locale}.home");
});
If the route field is used in Models the route cast must be specified:
use Ignite\Crud\Casts\Route;
$casts = [
'route' => Route::class
];
You can output the value of your route directly in Blade, for example in the href attribute like this:
<a href="{{ $model->route }}">My Link</a>
You can check if the route is active:
@if($model->route->isActive())
Hello World!
@endif
You may use the isActive
method to add optional classes to your link:
<a
href="{{ $model->route }}"
class="$model->route->isActive('active-class')"
>
My Link
</a>