Created
July 30, 2021 22:51
-
-
Save unisys12/6970663f6df6c9e0960d17b3df9a5f97 to your computer and use it in GitHub Desktop.
Passing Multiple Vars to View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
] | |
]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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