Gabax
Gabax

Reputation: 124

using sidebar in different blades

I'm a newbie in laravel and actually I have to write the sidebar in every blade of the application to make it work, but I'd like using a different sidebar depending on the section of the site where I am. So this is what I'm trying to do:

EDIT 1

layouts/main.blade.php

<div class="wrapper">
   <div class="sidebar" data-color="brown" data-active-color="danger">
      <div class="logo">
         <!-- Content -->
      </div>
      <!-- Sidebar -->
      @if(request()->is("{{ url('/')}}/{operator}"))
         @include('operator.sidebar')
      @else
         @include('stduser.sidebar')
      @endif
      <!-- End sidebar -->
   </div>

   <div class="main-panel">
      <!-- Navbar -->
      <nav></nav>
      <!-- End navbar -->

      <!-- Main content section -->
      @yield('main-panel')
      <!-- End main content section -->

      <!-- Footer -->
      <footer></footer>
      <!-- End footer -->
   </div>
</div>

stduser/dashboard.blade.php

@extends('layouts.main')

@section('main-panel')
   <!-- Main panel contents -->
@endsection

@section('extrajs')
   <!-- script contents -->
@endsection

stduser/sidebar.blade.php

<div class="sidebar-wrapper">
  <div class="user btn-rotate">
     <div class="photo">
        <i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
     </div>
     <div class="info">
        <a href="{{ url('/user') }}/profile">
           <span>
              {{ Auth::user()->name }}
           </span>
        </a>
        <div class="clearfix"></div>
     </div>
  </div>

  <ul class="nav">
     <li class="active btn-rotate">
        <a href="{{ url('/') }}">
           <i class="nc-icon nc-bank"></i>
           <p>Companies</p>
        </a>
     </li>
  </ul>
</div>

operator/sidebar.blade.php

<div class="sidebar-wrapper">
  <div class="user btn-rotate">
     <div class="photo">
        <i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
     </div>
     <div class="info">
        <a href="{{ url('/user') }}/profile">
           <span>
              {{ Auth::user()->name }}
           </span>
        </a>
        <div class="clearfix"></div>
     </div>
  </div>

  <ul class="nav">
     <li class="active btn-rotate">
        <a href="{{ url('/') }}/{{ $operator->id }}/about">
           <i class="fa fa-tachometer" aria-hidden="true"></i>
           <p>DashBoard</p>
        </a>
     </li>
     <li class="btn-rotate">
        <a href="{{ url('/')}}/{{ $operator->id}}/suppliers">
           <i class="fa fa-link" aria-hidden="true"></i>
           <p>Suppliers</p>
        </a>
     </li>
     <li class="btn-rotate">
        <a href="{{ url('/')}}/{{ $operator->id}}/products">
           <i class="fa fa-product-hunt" aria-hidden="true"></i>
           <p>Products</p>
        </a>
     </li>
  </ul>
</div>

This is how my views are structured:

enter image description here

Is there a way to make it work?

Upvotes: 1

Views: 1878

Answers (2)

eResourcesInc
eResourcesInc

Reputation: 1028

Based on your feedback to addi2113's answer, it seems like you're wanting to switch out the sidebar include based on which page you're on. There are several ways to do this. The simplest (yet least flexible) way to do this would be to show a certain sidebar based on the route. For instance, if you have a predictable route structure for all "operator" pages, such as example.com/operator/*, you could do the following by using an @if statement in your blade view. Like this:

@if(request()->is("/unique/url/pattern"))
    @include('operator.sidebar')
@else
    @include('stduser.sidebar')
@endif

Obviously, you can edit this to use any logic you want, but this is a somewhat simple way to handle this.

EDIT: Try this in your main.blade instead of using a section and yield:

<div class="wrapper">
  <div class="sidebar" data-color="brown" data-active-color="danger">
      <div class="logo">
         <!-- Content -->
      </div>
      <!-- Sidebar -->
      <div class="sidebar-wrapper">
        <div class="user btn-rotate">
           <div class="photo">
              <i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
           </div>
           <div class="info">
              <a href="{{ url('/') }}/profile">
                 <span>
                    {{ Auth::user()->name }}
                 </span>
              </a>
              <div class="clearfix"></div>
           </div>
        </div>
        @if(request()->is('/unique/url/pattern'))
             @include('operator.sidebar')
        @else
             @include('stduser.sidebar')
        @endif
     </div>
  </div>
</div>

EDIT 2:

Since it appears you are using a dynamic URL for the operator pages, you have two options. The first option is to make your operator routes more unique than they currently are so that you can use an asterisk to denote all routes of a current pattern. For instance, in routes/web.php, change your routes for operator pages to this type of pattern:

Route::get('/operator/{operator}/about','OperatorController@about')->name('operator-about');

By adding the operator slug into the url, you now have a UNIQUE path that you can reference. Then, in your main blade, you would reference all of the operator routes together like this:

@if(request()->is('/operator/*'))
     @include('operator.sidebar')

By making the URL unique, you have made a very simple way to reference all routes where you want to show the operator sidebar.

Another option, which is not as robust in my opinion, is to refer to the specific routes by naming. For instance, using the route I defined up above with the name of "operator-about", I could show the operator sidebar like this:

@if(Route::currentRouteName()=="operator-about")
     @include('operator.sidebar')

You would then expand upon this by explicitly defining all named routes that you would want to show the operator sidebar for. As you can probably tell, this will get messy if there are a lot of routes you want to include. I don't recommend to do it this way, but you can feel free to solve the problem however you want.

Upvotes: 1

addi2113
addi2113

Reputation: 154

you can include the blade file like so @include('layouts/sidebar_' . $sidebarName) and if you want to avoid errors when include doesnt exist you can use @includeIf('view.name', ['some' => 'data'])

So you have just the include statement and the sidebar content only once

Upvotes: 2

Related Questions