How to include files in a View based on the Route in Laravel?

1

I created a view inicio.blade.php where I put all the code that repeats on all pages and created other views that extend this view.

I've created routes for views by setting a name for them like this:

Route::get('/', 'PainelController@painel')->name('painel');
Route::get('calendario', 'PainelController@calendario')->name('calendario');

But in the inicio view view there are some files that are only used on some pages.

I've seen that it's possible to use conditional to check which route it is in, but the way I'm doing it is not working properly. Regardless of the route I'm in the condition always returns true.

I did it that way (I copied from the template the creates ):

@if (Route::has('calendario'))
<link rel="stylesheet" href="{{asset('css/owl.carousel.css')}}" type="text/css">
@endif

I even printed the route to see if it was correct and it returns true, but the condition always returns true:

<!--{{Route::currentRouteName()}}-->

If this is not the correct way could you tell me how to do it?

    
asked by anonymous 16.04.2018 / 20:25

1 answer

1
The Route::has checks to see if there is a route with this name in the route settings, it does not define exactly which route is currently being used, for this there is the currentRouteName() method that brings the name of the route used, adapting to your example:

@if (Route::currentRouteName() === 'calendario')
<link rel="stylesheet" href="{{asset('css/owl.carousel.css')}}" type="text/css">
@endif

Reference: Accessing The Current Route

    
16.04.2018 / 22:07