E
E
ettychel2019-06-05 21:47:54
Laravel
ettychel, 2019-06-05 21:47:54

Why is the authenticated user no longer defined in __construct?

I made a private variable, defined a value in the constructor, at first it worked, then it stopped for no reason, maybe I wrote something wrong?
And if in the method I prescribe what is equal, then everything works fine

class PostController extends BaseController{
    private $auth;
    
    public function __construct()    {
        parent::__construct();

        $this->auth = Auth::user();
}

public function index()    {
        // $this->auth = Auth::user();
        dd($this->auth);

I remove the comment in dd null
and everything is fine, there is a user

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ettychel, 2019-06-06
@ettychel

Decided in this way
In the controller from which PostController is inherited, the controller added

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

abstract class BaseController extends Controller
{
    public function authUser()
    {
        return Auth::user();
    }
}

then you can call $this->authUser()with all the consequences

J
JhaoDa, 2019-06-05
@JhaoDa

Wangyu that you have updated, but did not read the update guide. Section "Session In The Constructor" - https://laravel.com/docs/5.3/upgrade

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question