Why in the __construct ceased will be defined, an authenticated user?

Made a private variable defined in the constructor a value, first worked, then all of a sudden stopped, can something not so wrote?
And if what is the prescribed method, 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);


dd null
remove the comment and everything is fine, the user is
March 23rd 20 at 19:16
2 answers
March 23rd 20 at 19:18
Solution
We decided this way

The controller inherits from the controller PostController 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
Isn't it easier to get a user yourself? Why this method at all? - Landen_Littel35 commented on March 23rd 20 at 19:21
@Landen_Littel35, and if I have called in multiple methods? Why not just 1 register? - Alexandro.Huels commented on March 23rd 20 at 19:24
@Alexandro.Huels, well, let called you what? And yet you called Arr::get for the entire project, or even some crap. What will you do?

If you do this method - call authUser(), because user() nichrome is not clear where it comes from. And if you call authUser(), but isn't it easier to do auth()->user(), $this->auth->user() or Auth::user()? - Landen_Littel35 commented on March 23rd 20 at 19:27
@Landen_Littel35, uh, we argue about personal preferences?
Call it what you want, who said that direct just so and not otherwise?
Like to register every time to register, so please, what am I, your hands tied up?
And if you already do not understand and you live here don't know where to look for what it does out of context or not understand that it is there, put PhpStorm and do not worry - Alexandro.Huels commented on March 23rd 20 at 19:30
@Landen_Littel35, You will be calmer? - Alexandro.Huels commented on March 23rd 20 at 19:33
@Alexandro.Huels, I mean personal preference? It is the quality of the code. Me (exactly like anyone different from you people) do not understand that for a user, so the auth prefix is required.

And if it is mandatory, what is the difference between authUser or do the three options described above?

I don't want to guess what it does. And I don't want to click on methods to see what they are doing. I want readable code. It is not readable.

PS: add PS, and then I feel that predabissi to the words: "I" is all the normal people. - Landen_Littel35 commented on March 23rd 20 at 19:36
March 23rd 20 at 19:20
Wang, you are updated, and the update guide did not read. Section "Session In The Constructor" — https://laravel.com/docs/5.3/upgrade
Not been updated, as was 5.8.17(or 14) and remained - Alexandro.Huels commented on March 23rd 20 at 19:23
@Alexandro.Huels, well, as you can see, work is not must, but if and worked, I believe it's begovica to do as in the documentation. - Josiah.Murazik commented on March 23rd 20 at 19:26
Did exactly as the documentation did not help(
class PostController extends BaseController
{
 protected $auth;

 public function __construct()
{
parent::__construct();

 $this->middleware(function ($request, $next) {
 $this->auth = Auth::user()->auth;

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


In the end, again null - Alexandro.Huels commented on March 23rd 20 at 19:29
@Alexandro.Huels, the differences in native code have not noticed?
$this->auth = Auth::user()->auth;

and

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

What kind of Auth::user()->auth this? - gaetano_Turcot commented on March 23rd 20 at 19:32
@gaetano_Turcot, the documentation example
and the fact that the game works - Alexandro.Huels commented on March 23rd 20 at 19:35
@Alexandro.Huels, may not be in the documentation such an example, do not lie. - Josiah.Murazik commented on March 23rd 20 at 19:38
@Alexandro.Huels, you know, making manual Auth::user()->auth , and why the output is null? - gaetano_Turcot commented on March 23rd 20 at 19:41
<?php

namespace App\Http\Controllers;

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

class ProjectController extends Controller
{
/**
 * All of the current user's projects.
*/
 protected $projects;

/**
 * Create a new controller instance.
*
 * @return void
*/
 public function __construct()
{
 $this->middleware(function ($request, $next) {
 $this->projects = Auth::user()->projects;

 return $next($request);
});
}
}

This documentation - Alexandro.Huels commented on March 23rd 20 at 19:44
@Alexandro.Huels, okay.

@gaetano_Turcot, no he doesn't understand. - Josiah.Murazik commented on March 23rd 20 at 19:47
@gaetano_Turcot, even if not auth, changed to the example in the documentation zero sense - Alexandro.Huels commented on March 23rd 20 at 19:50
@Josiah.Murazik, I'm not from the ceiling it took, by the way the link you gave - Alexandro.Huels commented on March 23rd 20 at 19:53
@Alexandro.Huels, Yes, everything is already govnokod on, don't worry. - Josiah.Murazik commented on March 23rd 20 at 19:56
@Josiah.Murazik, perhaps dd is not so, but __construct exactly - Alexandro.Huels commented on March 23rd 20 at 19:59
@Josiah.Murazik, well, it may still prompt what not so? maybe stop - Alexandro.Huels commented on March 23rd 20 at 20:02
@gaetano_Turcot, @Josiah.Murazik, thanks, caught, finally, what it is
the comment above the variable is not read
Nitsche, eventually you will learn, in the beginning you need to honokowai, then to understand how much you have grown and why it should be so, and not differently - Alexandro.Huels commented on March 23rd 20 at 20:05

Find more questions by tags Laravel