vendredi 12 octobre 2018

Laravel Global Middleware Detected but Not Altering Model Records

I have the following middleware (LastSeen):

<?php

namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Factory as Auth;

class LastSeen
{
     /**
     * The authentication factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
     protected $auth;

     /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
     public function __construct(Auth $auth)
     {
         $this->auth = $auth;
     }
     /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
         if ($this->auth->check()) {
             $user = $this->auth->user();
             $user->last_activity = Carbon::now();
             $user->save();
        }
        return $next($request);
    }
}

This was mostly taken from this answer ( Check if user online laravel ) as a stop-gap solution to a project I am working on.

I do have a TIMESTAMP column in my users table labeled last_activity and this is my Kernel file global middleware:

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \App\Http\Middleware\LastSeen::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\ModifyHeadersMiddleware::class,
];

I'm not sure where to go next, I am just beginning on middleware and am still learning but not sure what I'm doing wrong...

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire