Apply Middleware to Route

1. Create a middleware

php artisan make:middleware MemberInfo

2. Modify middleware MmeberInfo
To share logged in member data to all views in the group

namespace App\Http\Middleware;

use Closure;

class MemberInfo
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        \View::share('member', auth()->user());
        
        return $next($request);
    }
}

3. Add this middleware to /app/Http/Kernel.php

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'member_info' => \App\Http\Middleware\MemberInfo::class,
    ];

4. Apply this middleware to route

        Route::group(['prefix' => 'member', 'middleware' => ['auth:web', 'member_info']], function(){
            Route::get('/', 'Member\[email protected]');
        });

After apply this middleware, every controller and view in the route group are able to access $member variable easily.

Reference: