I can not get the data of who is logged in

1

I have a doubt, I would like to recover the data that is logged in without having to go through the view, and use them in any view, so I decided to do in controller __construct plus laravel does not let me take user data .

<?php

namespace App\Http\Controllers;


use App\User;
use app\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->id;

            return $next($request);
        });

        $projects = $request->user();     
    }
}
    
asked by anonymous 12.08.2018 / 19:58

1 answer

1

You can use the View Composer to add a global variable, which all laravel views will inherit.

You need to set this in the App\Providers\AppServiceProvider::register method of your application:

    view()->composer('*', function($view) {
        $view['USUARIO'] = auth()->user();
    });

In the example above, all your views would default to $USUARIO containing auth() data.

Note : I particularly like to use global variables for views with names in the uppercase, to be able to differentiate from those passed by the view parameter.

    
12.08.2018 / 21:08