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

Asked

Viewed 247 times

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 created the routes for the views by setting a name for them this way:

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

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

I saw that it is possible to use conditional to verify which Route is, but the way I am doing are not working properly. Regardless of the route I am in the condition always returns true.

I did it this way (I copied from the model that the calf):

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

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

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

If this isn’t the right way, you could tell me how to do it?

1 answer

1


The Route::has checks if there is a route with this name in the route settings, does not define exactly what is the current route 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

  • 1

    Aeh! Saved the day. I’m starting to wonder. I had tried to use the Routename but gave error tmb, I believe it was the amount of equal signal I used that was wrong. But that way it worked now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.