herrh herrh - 21 days ago 7
PHP Question

How to get grouped routes in blade?

How can I get grouped routes in a blade template?

The grouped routes in php look like:

routes.php

...
//player section
Route::group(['prefix' => 'test'], function() {

Route::get('{slug}/{id}', [
'uses' => 'TestController@getTest',
'as' => 'getTest',
]);

Route::get('{slug}/{id}/report', [
'middleware' => 'auth',
'uses' => 'TestController@reportTest',
'as' => 'reportTest',
]);

});
...


In a blade template I get single routes by it's
'as'
string like

@if(Route::is('playTest'))
<!-- some html here -->
@endif


But how can I get all the routes grouped by prefix
'test'
in a blade template?

I want to prevent to write all the routes by it's name like:

(Route::is('getTest', 'reportTest'))

Answer

You should be able to do something like this:

@if(Request::route()->getPrefix() === 'test')
<!-- some html here -->
@endif

See: https://laravel.com/api/master/Illuminate/Routing/Route.html#method_getPrefix