File: /var/www/html/laravel/app/Models/ApiLog.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class ApiLog extends Model
{
public $timestamps = false;
protected $table = 'api_logs';
protected $fillable = [
'user_id',
'request',
'response',
'created_at',
];
public function createLog($request, $response, $userId = 0)
{
$dt = new Carbon();
$dt->setTimezone('UTC');
$headers = $request->header();
$data = [
'path' => $request->getPathInfo(),
'token' => $request->token,
'json' => $request->json,
'method' => $request->getMethod(),
'ip' => $request->ip(),
'http_version' => $_SERVER['SERVER_PROTOCOL'] ?? '',
'timestamp' => $dt->toDateTimeString(),
'headers' => $headers,
];
$this->user_id = $userId;
$this->request = json_encode($data);
$this->response = substr(json_encode($response), 0, 2000);
$this->created_at = $dt->toDateTimeString();
$this->save();
}
}