Skip to content

Instantly share code, notes, and snippets.

@unisys12
Created July 30, 2021 22:51
Show Gist options
  • Save unisys12/6970663f6df6c9e0960d17b3df9a5f97 to your computer and use it in GitHub Desktop.
Save unisys12/6970663f6df6c9e0960d17b3df9a5f97 to your computer and use it in GitHub Desktop.
Passing Multiple Vars to View
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
const CLIENT_ROLE = 3;
const TRAINER_ROLE = 2;
class DashboardController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$users = User::all()->count();
$clients = User::whereHas('roles', function (Builder $query) {
$query->where('role_id', 'like', CLIENT_ROLE);
})->count();
$trainers = User::whereHas('roles', function (Builder $query) {
$query->where('role_id', 'like', TRAINER_ROLE);
})->count();
// pass a multi-deminisional array to view containing each count
// query above.
return view('dashboard.index', [
'counts' => [
'users' => $users,
'clients' => $clients,
'trainers' => $trainers
]
]);
}
@extends('layouts.dashboard.index')
@section('content')
<header>
<h2>Info Panel</h2>
</header>
{{-- Model Counts --}}
<section>
@foreach ($counts as $key => $count)
<div>
<header>
<h3>{{ strtoupper($key) }}</h3>
<span>{{ $count }}</span>
</header>
</div>
@endforeach
</section>
@endsection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment