skip to Main Content

I want to have the same mechanism like this https://www.w3schools.com/html/default.asp but in laravel application
This is my code on routes/web.php

    Route::get('tutorial', function(){
    $tutorial = Tutorial::get();
        return view('tutorial.index')->with('tutorial', $tutorial);
    })->name('index-tutorial');

// Show one Tutorial by Id

Route::get('tutorial/{id}', function($id){
$tutorial = Tutorial::findOrFail($id);
return view('tutorial.show')->with('tutorial', $tutorial);
})->name('show-tutorial');

for my Blade template
tutorial/show.blade.php

 <div class="container">
@foreach($tutorial as $tutorial)
<h1>{{$tutorial->title}}</h1>
<p>{{$tutorial->title_description}}</p>
<p>{{$tutorial->title_lesson}}</p>
<div class="btn-group btn-group-lg d-flex justify-content-end mb-3" role="group">
    <form class="mx-3" action="{{route('delete-tutorial', $tutorial->id)}}" method="POST">
        @csrf
        @method('DELETE')
        <button class="btn btn-danger" name="Delete">Delete</button>
    </form>
    <form action="{{route('edit-tutorial', $tutorial->id)}}" method="GET">
        @csrf
    <button class="btn btn-primary" name="Edit">Edit</button>
</form>
@endforeach
</div>

tutorial/index.blade.php

 <main class="d-flex flex-nowrap">
 <div class="d-flex flex-column flex-shrink-0 p-3 text-bg-dark" 
 style="width: 280px;">
<a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md- 
  auto text-white text-decoration-     none">
  <svg class="bi pe-none me-2" width="40" height="32"><use 
  xlink:href="#bootstrap"></use></svg>
  <span class="fs-4 text-white">MySql Lessons</span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
  @forelse($tutorial as $link)
  <li class="nav-item">
    <a href="{{route('show-tutorial', $link->id)}}" class="nav- 
  link">
      <p class="text-white bg-dark">{{$link->title}}</p>
    </a>
  </li>
  @empty
    <p class="text-white bg-dark">No available lesson</p>
  @endforelse
</ul>
 </div> 

I been researching a lot about having this mechanism

This one is different from other questions because i don’t use controllers for this

2

Answers


  1. Chosen as BEST ANSWER

    Instead of using route closures, I manage to convert them to a TutorialController

    Route::resource('tutorial', TutorialController::class);
    

    for the aside bar routes/web

    view()->composer('layout.sidebar', function($view){
    $tutorial = Tutorial::all();
    $view->with('links', $tutorial);
    });
    

    layout/sidebar.blade.php

    <main class="d-flex flex-nowrap">
    <div class="d-flex flex-column flex-shrink-0 p-3 text-bg-dark" 
     style="width: 280px;">
      <a href="/" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto 
       text-white text-decoration-none">
        <svg class="bi pe-none me-2" width="40" height="32"><use 
       xlink:href="#bootstrap"></use></svg>
        <span class="fs-4 text-white">MySql Lessons</span>
      </a>
      <hr>
      <ul class="nav nav-pills flex-column mb-auto">
        @forelse($links as $link)
        <li class="nav-item">
          <a href="{{ route('tutorial.show', $link->id)}}" class="nav-link">
            <p class="text-white bg-dark">{{$link->title}}</p>
          </a>
        </li>
        @empty
          <p class="text-white bg-dark">No available lesson</p>
        @endforelse
      </ul>
    </div>
    </main>
    

    in my index.blade and show.blade you can add @include('layout.sidebar')

    @extends('layout.app')
    
     @section('title', "Show Tutorials")
    
     @section('content')
    
    
     @include('layout.sidebar')
     <div class="container">
    <h1>{{$tutorial->title}}</h1>
    <p>{{$tutorial->title_description}}</p>
    <p>{{$tutorial->title_lesson}}</p>
    <div class="btn-group btn-group-lg d-flex justify-content-end mb-3" 
            role="group">
        <form class="mx-3" action="{{route('tutorial.destroy', $tutorial- 
           >id)}}" method="POST">
            @csrf
            @method('DELETE')
            <button class="btn btn-danger" name="Delete">Delete</button>
        </form>
        <form action="{{route('tutorial.edit', $tutorial->id)}}" method="GET">
            @csrf
        <button class="btn btn-primary" name="Edit">Edit</button>
    </form>
    
     </div>
    
      @endsection
    

  2. Have you tried using example tamplates from Bootstrap?
    Check here https://getbootstrap.com/docs/5.3/examples/
    it’s quite easy actually, you just need to copy the html code from bootstrap tamplate and tweak here and there. use
    @include to add your desired components, @yield for your desired content page. and use @extends and @section inside your content pages.
    roughly like this.

    your main blade view

    <!doctype html>
    <html lang="en">
      <head>
       
      </head>
    
      <body>
        @include('partials.adminNav')
        <div class="row">
            @include('partials.adminSideBar')
        </div>
        <div class="container">
            @yield('container')
        </div>
      </body>
      @include('partials.footer')
    </html>
    

    for your side bar you just need to put links for your desired page

    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
          <div class="position-sticky pt-3 sidebar-sticky">
            <ul class="nav flex-column">
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="#">
                  <span data-feather="home" class="align-text-bottom"></span>
                  //Home
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="//your links/route for page content">
                  <span data-feather="file" class="align-text-bottom"></span>
                  //links name
                </a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="//your links/route for page content">
                  <span data-feather="edit" class="align-text-bottom"></span>
                  //Links name
                </a>
              </li>
            </ul>
          </div>
        </nav>
    

    in like this in your content pages

    @extends('layouts.adminMain')
    
    @section('container')
    
    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
        <div class="py-4">
          // your content here
        </div>
    </main>
    
    @endsection
    

    make use of the @include and @yield build in function. this way you’ll have a consistent navbar/sidebar but can changes the content within.
    Hope this works for you 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search