Declare a variable to be used in all methods Laravel - laravel

What is the best way to declare a variable to be used in all the methods within my controllers and my models:
example .. I want to replace:
Auth::user()
by:
$this->user
What would be the best way to do it?

For Laravel 5.3.4+
Declare the property in your controller and then you can do it like this in its __construct method:
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
For versions below, you can just $this->user = Auth::user(); inside __construct.
Reference: https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors

If you attempt to set a user in your Controller.php's __construct method you will find it won't work.
This won't work:
public function __construct()
{
$this->user = auth()->check() ? auth()->user() : null;
}
This is by design, according Taylor, the framework's creator. He discourages setting your user in the base controller - but, it's up to you how you want to do things. There is a way around this. If you wanted $this->user to be accessible in every controller, you would add this to your Controller.php file:
protected function setUser()
{
$this->user = auth()->check() ? auth()->user() : null;
}
Then in your __construct method call it:
public function __construct()
{
$this->setUser();
}
Now, you can access this->user from any controller method. It will be null if not set, but if you are using Laravel's standard auth guard you shouldn't have much of a problem with unauthenticated users getting through.

Related

How to get current user id in constructor in laravel?

I am using laravel 5.7, but i can't get current user id in __construct().
I also tried Auth:id(), but it also not working.
How to get current user id in constructor?
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public $id;
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->id = Auth::user()->id;
return $next($request);
});
dd($this->id);
}
}
Current output is null.
You can only access the session in the closure. Just refactor your code to this:
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->id = Auth::user()->id;
dd($this->id);
return $next($request);
});
}
You can now use the value $this->id in your controller methods.
In the example in your question, after you've set the value $this->id, you continue with the request. Since you try to access $this->id outside of the scope of the closure, it still is null in the datadump.
After return you will not go to next statement that's why it is not print.
If you want to use this in view then no need to pass in view you can simply access logged user id like this
{{Auth->user->id}}
if you wan to use this in controller make sure you are logged in.
Sometime session expired then you will not get user id
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public $id;
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
$this->id = Auth::user()->id;
dd($this->id);
return $next($request);
});
}
}
The easiest solution is to create a middleware and call it later in the constructor.
php artisan make:middleware FoobarMiddleware
I recommend putting an alias in Kernel.php
protected $routeMiddleware = [
...
'foobar' => \App\Http\Middleware\FoobarMiddleware::class,
]
Constructor:
public function __construct()
{
$this->middleware('auth');
$this->middleware('foobar');
}
I recommend changing the focus of how you are creating everything

laravel middleware within __construct

I'm using https://github.com/appstract/laravel-multisite which is working fine.
Within my controllers methods, I can var_dump current_site() and the correct information is displayed.
My issue is that within my controller __construct method the current_site function returns null i.e the multi_site object hasn't yet been setup.
public function __construct()
{
//this returns null
var_dump(current_Site());
}
public function index()
{
//this works
var_dump(current_Site());
}
I assuming the constructor is called before the routing has been done and therefore thats the issue, but I wanted to only call the current_site function once and have the controller know which site was being used from the start, so all methods etc would know.
I think this is just a lack of knowledge on my part rather than a code issue...any guidance ?
My route groups as like this
'domain' => 'dealer1.'.config('multisite.host'),
'as' => 'dealer1.',
'middleware' => 'site:dealer1'
I know this is todo with middleware not being done before the __construct being called but should I call it by doing
$this->middleware('CurrentSite');
wWhat step should I do next?
Edit
I've changed my code to
public function __construct()
{
$this->middleware(function ($request, $next) {
//this dumps correctly
var_dump(current_Site() );
$this->currentSite = current_Site();
return $next($request);
});
//this is null
//var_dump($this->currentSite);
//die("here");
}
You could try something like:
public function __construct()
{
$this->middleware(function ($request, $next) {
var_dump(current_Site());
return $next($request);
});
}
https://laravel.com/docs/master/controllers#controller-middleware
Hope this helps!

Laravel 5.4 Sessions and Auth::user() not available in controller's constructor

I would like to use a User class throught the application. So, I would like to create CustomUser and then inject it into controllers that need it (it would be most of them).
Now, I create an empty instance in serviceprovider. Next, I want to fill it with data that are already saved in Auth::user(). After long time I have not found where to do it.
Auth::user() is empty in middlewares, but is filled with the user data in controllers. I am missing the step where Laravel queries the database and fills Auth:user() with data. I want to avoid making the same query again.
Thanks for any help!
You can use base controller with __get() method. For example:
class Controller
{
public function __get(string $name)
{
if($name === 'user'){
return Auth::user();
}
return null;
}
}
And in the child controllers can call $this->user
Since Laravel 5.3, you do not have access to sessions in controller constructors. This is because the middleware has not been run yet. I know it's difficult to locate, but in the migration documentation from 5.2 > 5.3 (you're probably on 5.4), it shows that the proper way to resolve data from sessions (which auth() is just a wrapper around a session() call to get the user), is to use the following method:
class MyController extends Controller {
protected $user;
public function __construct() {
$this->middleware(function ($request, $next) {
$this->user= auth()->user();
return $next($request);
});
}
}
Then $this->user will reference the auth user to any methods inside of this controller.
Hopefully his helps.
In Laravel 5.6 i used this
$this->middleware(function ($request, $next) {
$id = Auth::user()->id;
$res = $this->validateAnyFunction($id);
if(!$res){
//to redirect to any other route
return $next(redirect()->route("any")->with("failed","Invalid")->send());
}
//this is used to proccess futher funcitons of controller
return $next($request);
});

Laravel 5.3: How to use Auth in Service Provider?

I am passing a value in shared view by taking value from table. I need to know user ID for the purpose but Auth::check() returns false. How do I do it? Below is code:
public function boot()
{
$basket_count = 0;
if (Auth::check()) { //always false
$loggedin_user_id = Auth::user()->id;
$basket_count = Cart::getBasketCount();
}
view()->share('basket_count', $basket_count);
}
OK turns out that ServiceProviders are not place for such things. The best thing is a Middleware. So if you want to call Auth, create middleware and pass value to views.
public function handle($request, Closure $next)
{
$basket_count = 0;
if ($this->auth) { //always false
$loggedin_user_id = $this->auth->user()->id;
$basket_count = Cart::getBasketCount($loggedin_user_id);
}
view()->share('basket_count', $basket_count);
return $next($request);
}
You can use authentication directly in the controller file. Adding it in the middleware is a cleaner way of doing the authentication.
For eg. In CategoriesController.php
...
class CategoryController extends Controller {
/**
* CategoryController constructor.
*/
public function __construct()
{
$this->middleware('auth');
}
...
If you want to have a look at a complete example
http://deepdivetuts.com/basic-create-edit-update-delete-functionality-laravel-5-3

Making global user variable in laravel 5.3

I have this code
Controller.php
public function __construct()
{
$this->user = Auth::user();
view()->share('user', $this->user );
}
HomeController.php
public function __construct()
{
$this->middleware('auth');
parent::__construct();
}
It didn't work so I debug it with debugger and I saw that Auth::user() only return a user after finishing constructing.
i.e in HomeController#index() function
Any idea how to solve it ?
That used to work in laravel 5.2, however in laravel 5.3 you can no longer access session variables or the authenticated user in your controller's constructor. So they provided a work around that looks like this:
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors
You can use auth()->user() or Auth::user() globally without creating any variables. You can use it even in views.
It's an auful idea to dublicate and store whole User object in a variable, session etc.

Resources