I would like to get the current logged in user record so I can retrieve a language id from the database and thus call my language function to get language tokens.
I have seen lots of people saying user Auth::user()->fieldName to get the field however when I try this I just get the following error:
Class 'App\Auth' not found
I am going to need easy access to this throughout my application so I have a couple of questions:
What location is this? so what would be the "use blah/blah/Auth"?
How do make it so this is always available without having to declare it?
Apologies if I am being thick - I am quite new to Laravel...
Also, the class is already available, the message really means that it can't find it in the current namespace. If you don't feel like importing the class (http://php.net/manual/en/language.namespaces.importing.php), you can add a backslash to tell laravel to look in the upper namespace.
@aktolman You could get the currently logged in user in your base controller:
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Contracts\Auth\Guard;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
public function __construct(Guard $auth)
{
$this->currentUser = $auth->user();
}
}
Note: you will need to call parent::__construct() in any extending controller classes.
@bestmomo I prefer to follow best practices and not have too much “magic” going on in my code. Makes re-factoring easier too, rather than hinging code on façades and global helper functions.