create a middleware in laravel code example

Example 1: laravel make middleware

php artisan make:middleware CheckAge

Example 2: middleware command in laravel

php artisan make:middleware NameOfTheMiddleware

Example 3: laravel middleware

<?php

namespace App\Http\Middleware;

use Closure;

class CheckAge
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {
            return redirect('home');
        }

        return $next($request);
    }
}

Example 4: laravel set middleware default

If you want a middleware to run during every HTTP request to your application, list the middleware class in the $middleware property of your app/Http/Kernel.php class.