How to retrieve data from the database and display it in a navigating menu?

advertisements

I am using laravel 5.3 I have a form that helps me create new projects. I can also display all the projects I have in DB with all the attributes. Now, I need to display responding navigation menu in the left on the page that contains the following :

Domaines : Domaine 1 : project 1 project 2 Domaine 2 : project A project B

I want that once I click on project there's table that display all the information of that project.

this is "index.blade.php" when I only was trying to display the projects without any ordre :

@extends('layouts.app')
@section('content')
  <div class="row">
    <div class="col-lg-12 margin-tb">
      <div class="pull-left">
         <h2>Projects CRUD</h2>
      </div>
    <div class="pull-right">
        @permission('pro-create')
        <a class="btn btn-success" href="{{ route('pros.create') }}"> Create New Project</a>
        @endpermission
      </div>
     </div>
    </div>
    @if ($message = Session::get('success'))
       <div class="alert alert-success">
          <p>{{ $message }}</p>
       </div>
    @endif
    <table class="table table-bordered">
       <tr>
         <th>No</th>
         <th>title</th>
         <th>code</th>
         <th>domain_id</th>
         <th width="280px">Action</th>
       </tr>
       @foreach ($pros as $key => $pro)
         <tr>
           <td>{{ ++$i }}</td>
           <td>{{ $pro->title }}</td>
           <td>{{ $pro->code }}</td>
           <td>{{ $pro->domain_id}}</td>
           <td>
           <a class="btn btn-info" href="{{ route('pros.show',$pro->id) }}">Show</a>
          @permission('pro-edit')
           <a class="btn btn-primary" href="{{ route('pros.edit',$pro->id) }}">Edit</a>
          @endpermission
          @permission('pro-delete')
           {!! Form::open(['method' => 'DELETE','route' => ['pros.destroy', $pro->id],'style'=>'display:inline']) !!}
           {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
           {!! Form::close() !!}
          @endpermission
         </td>
        </tr>
       @endforeach
      </table>
      {!! $pros->render() !!}
@endsection

and this is what I was trying to add to have the "responding navigation menu" :

<div class="nav-side-menu">
  <div class="brand">Menu</div>
  <i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
  <div class="menu-list">
    <ul id="menu-content" class="menu-content collapse out">
        <li>
            <a href="#">
             <i class="fa fa-dashboard fa-lg"></i> Dashboard
            </a>
        </li>
        <li  data-toggle="collapse" data-target="#domains" class="collapsed active">
            <a href="#"><i class="fa fa-gift fa-lg"></i> Domains <span class="arrow"></span></a>
        </li>
          <ul class="sub-menu collapse" id="domains">
             <li class="active"><a href="#">Domain 1</a></li>
             <li><a href="#">Domain 2</a></li>
             <li><a href="#">Domain 3</a></li>
             <li><a href="#">Domain 4</a></li>
             <li><a href="#">Domain 5</a></li>
          </ul>
            <li data-toggle="collapse" data-target="#responsible" class="collapsed">
             <a href="#"><i class="fa fa-globe fa-lg"></i> Responsibles <span class="arrow"></span></a>
            </li>
             <ul class="sub-menu collapse" id="responsible">
               <li>Mr.name1</li>
               <li>Mr.name2</li>
               <li>Mr.name3</li>
             </ul>
    </div>
</div>

My question is how to build a relation between my DB and the view? how can I retrieve data from DB (projects table) and display it in the view ?


You create a model for your table "Example"

Model

class Example extends Model {
    protected $table = 'Example';
    protected $fillable = ['column1', 'column2'];
    protected $primaryKey = 'id';
}

Don't forget to specify your connection in /config/database.php

Now you have your model you can create a Controller, I would suggest a resource controller.

use App\Model\Example; class ExampleController extends Controller {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    // This is the one we need, because it will be shown on GET domain.com/example

$examples = Example::query()->get();
return view('exampleview', ['passed_data' => $examples]);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}

}

Define your route. Because you want to display frontend you choose routes/web.php and define

Route::resource('example', 'ExampleController');

Now you also have to make your blade under resources\views called exampleview.blade.php (as defined in the controller)

there you can use the passed data in blade syntax: exampleview.blade.php @foreach ($passed_data as $single_example) {{ $example }} @endforeach

To make your menu change based on the view you are in you should define a

@stack('menustack') inside your menu blade

and push new element from the "lower" blade with

@push('menustack')
<div> new menu div </div>
@endpush