Laravel is an open source framework whose utility is to develop web applications and services using one of the most popular languages on the internet: PHP. Laravel's philosophy is quite simple: develop PHP code in an elegant way and focus on simplicity, thus avoiding spaghetti code.
When we say "spaghetti code" we are referring to that which, in its construction, resembles a pasta dish. It's a derogatory way of naming complex flow structure control programming code, like it's a bunch of strings, dating back to the 1960s.
One of the intentions of Laravel and its developers lies in taking advantage of the best of other frameworks, as well as the features of the latest versions of the PHP programming language.
Using a Blade Template Engine
Although some developers consider PHP to be a template engine in itself, the syntax did not evolve in recent years, unlike newer template engine proposals, such as the one used by Laravel. Blade, one of the simplest but very powerful compared to other template engines, adds zero overhead to the web application and does not prevent you from using simple PHP code in views.
Eloquent, the ORM that includes Laravel
Laravel works with Eloquent, an ORM that offers web developers a simple implementation of ActiveRecord, which makes their interaction with databases easy and therefore requires less time.
Open source and a broad community
Although many PHP frameworks come with a price tag, Laravel is an open source framework for web developers. In addition, it has a powerful community of developers who support it, making it more advanced and flexible.
Laravel's extensive libraries oriented to objects
Laravel's PHP framework is packed with many built-in object-oriented libraries that are packed with amazing features for developers.
One of the most sought-after features in networks by experts in this framework is how to know if a user is online or offline. Let's go step by step:
First, configure the database
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password
The next step is to install auth and then add a column in the users table
php artisan make:migration add_last_seen_to_users_table --table=users
Successfully run the above command after adding the column in the users table. Open the migration file and put the code below.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddLastSeenToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->timestamp('last_seen')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->timestamp('last_seen'); }); } }
Create Middleware
php artisan make: middleware LastSeenUserActivity
Then open the LastUserActivity.php file and paste the following code:
<?php namespace App\Http\Middleware; use Closure; use App\User; use auth; use Cache; use Carbon\Carbon; class LastSeenUserActivity { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (Auth::check()) { $expireTime = Carbon::now()->addMinute(1); // keep online for 1 min Cache::put('is_online'.Auth::user()->id, true, $expireTime); //Last seen User::where('id', Auth::user()->id)->update(['last_seen' => Carbon::now()]); } return $next($request); } }
Then add middleware to the kernel file
protected $middlewareGroups = [ 'site' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \App\Http\Middleware\LastSeenUserActivity::class, ], 'api' => [ 'throttle:60,1', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ];
Add the route
Route::get('/status', 'UserController@show')
Create the controller
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use Cache; use Carbon\Carbon; class UserController extends Controller { public function show() { $users = User::all(); return view('status', compact('users')); } }
Create View File
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header bg-info text-white text-center"><h2><b>How to Check User Online Status & Last Seen in Laravel - NiceSnippets.com</b></h2></ div> <div class="card-body"> @php $users = DB::table('users')->get(); @endphp <div class="container"> <table class="table table-bordered"> <head> <tr> <th>Name</th> <th>Email</th> <th>Status</th> <th>Last Seen</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{$user->name}}</td> <td>{{$user->email}}</td> <td> @if(Cache::has('is_online' . $user->id)) <span class="text-success">Online</span> @else <span class="text-secondary">Offline</span> @endif </td> <td>{{ \Carbon\Carbon::parse($user->last_seen)->diffForHumans() }}</td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> </div> @endsection
And that's it, so you can see if the user is online or offline.