0

I have a code in my controller of Laravel-5.8

public function customers()
{
try { 
    $userCompany    = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;

    $countCustomers = Customers::where('company_id', $userCompany)->count();
    $customers = Customers::where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
    return view('customers')
            ->with('countCustomers', $countCustomers)
            ->with('countCustomers', $countCustomers);
    } catch (Exception $exception) {
        Session::flash('error', 'Action failed! Please try again');
        return back();
        }        
}

The code counts the numbers of customers and also lists the top 5 customers

route\web.php

Route::get('/customers', 'HomeController@customers')->name('customer-default');

I want to render this code in the layouts\header.php as shown below.

layouts\header.blade

  <li class="nav-item dropdown">
    <a class="nav-link" data-toggle="dropdown" href="#">
      <i class="far fa-bell"></i>
      <span class="badge badge-warning navbar-badge">0</span>
    </a>
    <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
      <span class="dropdown-item dropdown-header">You have {{$countCustomers}} Customers</span>
      <div class="dropdown-divider"></div>
                    @foreach($customers as $customer)
                {
                    {{ $customer->first_name }},
                }
                @endforeach
      <div class="dropdown-divider"></div>
      <a href="#" class="dropdown-item dropdown-footer">See All Notifications</a>

    </div>
  </li>        

But since the header in the layout don't have controller.

How do I achieve this?

Thank you.

mikefolu
  • 1,203
  • 6
  • 24
  • 57
  • Does this answer your question? [How to pass data to all views in Laravel 5?](https://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5) – Oleg Nurutdinov Apr 25 '20 at 20:10

1 Answers1

0

you can do this by using middlware

1- create middlware with any name

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\View;

class GlobalData
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      $userCompany    = Auth::user()->company_id;
      $userEmployee = Auth::user()->employee_id;

      $countCustomers = Customers::where('company_id', $userCompany)->count();
      $customers = Customers::where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
        View::share('customers', $customers);
        return $next($request);
    }
}

2- register your middleware in Kernal.php

protected $routeMiddleware = [
 .
 ...
 'globaldata' => GlobalData::class,
 ]

3-now group your routes with globaldata middleware

Route::group(['middleware' => ['globaldata']], function () {
//  another routes
    Route::get('/customers', 'HomeController@customers')->name('customer-default');
}
Mortada Jafar
  • 3,529
  • 1
  • 18
  • 33