I created a __construct
function in the Controller.php of Laravel 5.
And this function is obviously running even on the Login screen in Auth .
Is there any way I can put as a condition, run the function inside __construct
only when the user is logged in?
I know a way to do, at the beginning of the function I put:
if(Auth::check())
# Roda Função
But is there another way? That way above every time it will do the verification.
__construct
public function __construct(){
if(Auth::check()){
# Monta Menu e Sub Menu
$menu = Menu::orderBy('posicao')->get();
$arrMenu = array();
foreach($menu as $itemMenu){
$arrMenu[$itemMenu->titulo]['titulo'] = $itemMenu->titulo;
$arrMenu[$itemMenu->titulo]['link'] = $itemMenu->link;
$arrMenu[$itemMenu->titulo]['icon'] = $itemMenu->icon;
$arrMenu[$itemMenu->titulo]['slug'] = $itemMenu->slug;
$submenu = Submenu::whereIdMenu($itemMenu->id)->orderBy('posicao')->get();
foreach($submenu as $itemSubMenu){
$arrMenu[$itemMenu->titulo]['submenu'][$itemSubMenu->titulo]['titulo'] = $itemSubMenu->titulo;
$arrMenu[$itemMenu->titulo]['submenu'][$itemSubMenu->titulo]['link'] = $itemSubMenu->link;
}
}
# Total de Chamados Agrupado por Status no Menu
if(Auth::user()->id_role == 1){
$chamados = DB::table('chamados')
->rightJoin('status', 'status.id', '=', 'chamados.id_status')
->select(DB::raw('count(chamados.id) as total, status, status.id as id_status'))
->orderBy('status.id')
->groupBy('status')
->get();
}
else{
$chamados = DB::table('chamados')
->select(DB::raw('count(chamados.id) as total, status, status.id as id_status'))
->orderBy('status.id')
->groupBy('status')
->rightJoin('status', function($join){
$join->on('status.id', '=', 'chamados.id_status')
->where('id_departamento', '=', Auth::user()->id_departamento)
->where('id_user', '=', Auth::id())
->orWhere('id_user', '=', NULL);
})
->get();
}
$totalFinal = 0;
foreach ($chamados as $key => $value){
$totalChamado[$value->id_status] = $value->total;
$totalFinal += $value->total;
}
# Coloca em Array o Menu e o Total de Chamados para Colocar no Share
$arrShare = array(
'menu' => $arrMenu,
'chamados' => $totalChamado,
'total' => $totalFinal
);
view()->share('arrShare', $arrShare);
}
}