vendredi 14 février 2020

How to pass data to layouts without passing data to every page controller?

I have a custom layout blade that have multi level navigation menu and 2 page blade that extends the layout. I want to create a side navigation menu that loop through data. So if I created a new side nav, I don't have to mess the layout.

But the problem is, I don't know to pass the data to the layout itself.

For now, I just pass the data on every page controller that I have. I think it would be better if I just pass the data to the layout and call any page that extends the layout without passing the data again on every page controller.

layout blade (user.blade.php)

<li class="nav-item has-treeview">
   <a href="#" class="nav-link">
      <i class="nav-icon fas fa-wifi"></i>
      <p>
         Hotspot Voucher
         <i class="right fas fa-angle-left"></i>
      </p>
   </a>
   <ul class="nav nav-treeview">
      <li class="nav-item has-treeview">
         <a href="#" class="nav-link">
            <i class="far fa-circle nav-icon"></i>
            <p>
               Floors
               <i class="right fas fa-angle-left"></i>
            </p>
         </a>
         <ul class="nav nav-treeview floorlist">
            @foreach ($floors as $floor)
            <li class="nav-item">
               <a href="" class="nav-link">
                  <i class="far fa-dot-circle nav-icon"></i>
                  <p></p>
               </a>
            </li>
            @endforeach
         </ul>
       </li>
    </ul>
 </li>

Home blade (home.blade.php)

@extends('layouts.user')

@section('content')
    <!-- Content Header (Page header) -->
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Dashboard</h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">
                            <a href="">Home</a>
                        </li>
                        <li class="breadcrumb-item active">Dashboard</li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">   
                        <div class="panel-body">
                            @component('components.who')
                            @endcomponent
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- /.content -->
@endsection

Floor Blade (floor.blade.php)

@extends('layouts.user')

@section('content')
    <!-- Content Header (Page header) -->
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark"></h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">
                            <a href="">Home</a>
                        </li>
                        <li class="breadcrumb-item">
                            <a href="">Dashboard</a>
                        </li>
                        <li class="breadcrumb-item active"></li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">   
                        <div class="panel-body">
                            @foreach ($get_rooms as $get_room)
                                
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- /.content -->
@endsection

HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Floor;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $floors = Floor::select(
                'floors.id AS floor_id', 
                'floors.name AS floor_name'
                )
                ->where('floors.co_id', Auth::user()->co_id)
                ->get();

        return view('home')->with('floors', $floors);
    }
}

FloorController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Floor;
use App\Room;

class FloorController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($fid)
    {

       $floors = Floor::select(
                'floors.id AS floor_id', 
                'floors.name AS floor_name'
                )
                ->where('floors.co_id', Auth::user()->co_id)
                ->get();

        $get_floor_details = Floor::where(['floors.co_id' => Auth::user()->co_id, 'id' => $fid])->get();

        foreach($get_floor_details as $get_floor)
        {
            $get_floor_name = $get_floor->name;
        }        

        $get_rooms = Room::select(
            'rooms.id AS room_id', 
            'rooms.name AS rooms_name',
            'floors.name AS get_floor_name'
            )
            ->join('floors', 'floors.id', '=', 'rooms.floor_id')
            ->where(['rooms.co_id' => Auth::user()->co_id, 'rooms.floor_id' => $fid])
            ->get();

        $data = [
                'floors' => $floors,
                'get_floor_name' => $get_floor_name,
                'get_rooms' => $get_rooms
        ];

        return view('floor')->with($data);
    }
}

@Andy Song solution

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use View;
use Auth;
use App\Floor;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('layouts.user', function ($view) 
        {
            $floors = Floor::select(
                'floors.id AS floor_id', 
                'floors.name AS floor_name'
                )
                ->where(['co_id' => Auth::user()->co_id])
                ->get();
            $view->with('floors', $floors);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/31SxWEl
via IFTTT

Aucun commentaire:

Enregistrer un commentaire