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 for only a particular page without duplicating layouts.

Here is how is solved it with named routes:

So I ne…


This content originally appeared on DEV Community and was authored by Chidiebere Chukwudi

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 for only a particular page without duplicating layouts.

Here is how is solved it with named routes:

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

My web.php

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

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>

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.


This content originally appeared on DEV Community and was authored by Chidiebere Chukwudi


Print Share Comment Cite Upload Translate Updates
APA

Chidiebere Chukwudi | Sciencx (2021-04-28T14:35:41+00:00) How To Check for a particular Route then apply condition in laravel (blade). Retrieved from https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/

MLA
" » How To Check for a particular Route then apply condition in laravel (blade)." Chidiebere Chukwudi | Sciencx - Wednesday April 28, 2021, https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/
HARVARD
Chidiebere Chukwudi | Sciencx Wednesday April 28, 2021 » How To Check for a particular Route then apply condition in laravel (blade)., viewed ,<https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/>
VANCOUVER
Chidiebere Chukwudi | Sciencx - » How To Check for a particular Route then apply condition in laravel (blade). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/
CHICAGO
" » How To Check for a particular Route then apply condition in laravel (blade)." Chidiebere Chukwudi | Sciencx - Accessed . https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/
IEEE
" » How To Check for a particular Route then apply condition in laravel (blade)." Chidiebere Chukwudi | Sciencx [Online]. Available: https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/. [Accessed: ]
rf:citation
» How To Check for a particular Route then apply condition in laravel (blade) | Chidiebere Chukwudi | Sciencx | https://www.scien.cx/2021/04/28/how-to-check-for-a-particular-route-then-apply-condition-in-laravel-blade/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.