How is exactly to display current login username in a web-application based or powered by Laravel framework. This is an article which is written in order to perform and describe about howto display current login username in the web-application powered by Laravel framework. This article has a deep connection with the one written in another article titled ‘Laravel login page example tutorial for Laravel 5.3’ where it can be found in this link.
So, after setting up authentication feature for the web application, there is a need to precisely display information of the current username in the web-application based on Laravel framework. It is a simple steps involving on editing the blade view template file located in the resources/views folder or the folder which is dedicated to store the blade view template.
The one which is needed to be edited is a file named ‘home.blade.php’ located in ‘/root-folder-laravel-web-app/resources/views/’ normally. To prove that the information about the current login user can be presented in the ‘home.blade.php’ below is the content of the ‘home.blade.php’. Precisely it is in the app.blade.php located in the layout folder. It is because in the ‘home.blade.php’ file, it is extending :
@extends('layouts.app')
And the content of app.blade.php which is presenting the information of the current logged in user, specifically its name and its email is shown below :
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::user()->name }} <span class="caret"></span> [ {{ Auth::user()->email }} ] </a>
The information displayed is the name and the email of the current login username which can be shown below :
As shown in the right top of the page there is an info regarding the current logged user which is presented as follows :
myuser [[email protected]]
Not only depending on default Laravel blade view template tag ‘{{ }}’ which is shown in this line below :
{{ Auth::user()->name }}
and also in this line :
{{ Auth::user()->email }}
There is another method which can be used to display the information of the current login user. It is using the php script as shown below :
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::user()->name }} <span class="caret"></span> [ {{ Auth::user()->email }} ] Created at : <?php echo Auth::user()->created_at; ?> Updated at : <?php echo Auth::user()->updated_at; ?> </a>
So, below is the image of the current home page after adding the php script above displaying additional information about the current logged in user :
As shown in the above home page, there is an additional information regarding current logged in user as shown below :
Created at : xxxx-xx-xx xx:xx:xx Updated at : xxxx-xx-xx xx:xx:xx
The additional script which is used based on php script for presenting the information regarding the current logged in user can be shown precisely in the following line of codes :
<?php echo Auth::user()->created_at; ?> <?php echo Auth::user()->updated_at; ?>
The additional information of the current logged in information can be selected based on the field exists in the users table created when performing ‘php artisan migrate’ in order to create additional tables to support authentication feature in the web-apps powered by Laravel framework. Below are the fields possible to be viewed :
mysql> desc users; +----------------+-----------------+------+---+-------+---------------+ | Field | Type | Null |Key|Default| Extra | +----------------+-----------------+------+---+-------+---------------+ | id | int(10) unsigned| NO |PRI| NULL |auto_increment | | name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO |UNI| NULL | | | password | varchar(255) | NO | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +----------------+-----------------+------+---+-------+---------------+ 7 rows in set (0,00 sec) mysql>
So, based on the syntax pattern of the following line of code :
Auth::user()->field_name Description : Auth : It is one of the class name handled for Authentication feature user() : It is the method called to summon the current user logged in field_name : It is the attribute of the current user logged in and it is also represented in the field of the users table.
There are several information that can also be retrieved from the current user logged in based on the field exist in table ‘users’.
One thought on “Display Current Login username Laravel”