Reputation: 181
I have a laravel 5.2 app with authentication.I have built a custom global middleware that updates a "last_seen" column on the users table for each user.
Note: It is a middleware that runs after the request.
I use the "UTC" timezone so that there would be no errors when calculating the difference anywhere in the world.
Everything works fine and as expected,until i log out.When I log out,the column is updated with the time in my current timezone "romania". I have no idea why that is,since I never declared a timezone other than "UTC".
Here is my middleware code:
<?php
namespace App\Http\Middleware;
use Auth;
use Closure;
use Carbon\Carbon;
class LastSeenMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
if(Auth::check()){
Auth::user()->last_seen = Carbon::now();
Auth::user()->save();
}
return $response;
}
}
Upvotes: 3
Views: 487
Reputation: 181
I have found the solution.
Apparently,in my table schema declaration,I forgot to set the last_seen column nullable,and therefore,it updated with the current server timestamp even though I did not intended that.
I found that out by peeking into the source code for the default created_at and updated_at columns.
Upvotes: 2