I can not get the data of who is logged

Asked

Viewed 173 times

1

I have a question, 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 the controller __construct The more the Laravel doesn’t let me take the 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();     
    }
}
  • Your problem may be similar to this one: https://answall.com/questions/127178/p%C3%A1ginas-do-Laravel-5-2-redirecting-no-show-no-validator errors%C3%A7%C3%A3o/127187#127187

1 answer

1

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

You need to define this in the method App\Providers\AppServiceProvider::register of its application:

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

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

Observing: particularly like to use global variables for views with high-case names, to be able to differentiate from those that are passed by view parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.