DEV Community

Chidiebere Chukwudi
Chidiebere Chukwudi

Posted on • Edited on • Originally published at jovialcore.tech

6 4

How To Check for a particular Route then apply condition in laravel (blade)

I want to use a single nav bar (in layouts/app.blade.php) for all my pages in laravel but I wanted a way I could show a particular search form right on the navbar for only a particular page without duplicating layouts.

Here is how I solved it with named routes:

So I need to check if a URL is exactly like, e.g, "page" and then I show something.

My web.php

Route::get('/pages', function () {
    return view('page');
})->name('page');
Enter fullscreen mode Exit fullscreen mode

My layouts/app.blade.php

<nav class="navbar">
   <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="#">Coventions</a>
          </li>
        </ul>

   @if (\Route::current()->getName() == 'page') 

<form >
<input name="s" type="search">
</form>

@endif 

<button> Upload </button>

</nav>
Enter fullscreen mode Exit fullscreen mode

So whats happening above: I simply mean that if the route is same as 'page' every other thing under the blade @if (which is the search form )condition will show else, it should just ignore and display the rest.

I hope it helps.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay