<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class SqlLog extends Model
{
    use HasFactory;

    protected $fillable = [
        'ip',
        'user_id',
        'name',
        'req_sql',
        'data',
        'time',
    ];


    public static function LogSql(){
        try {
            $request=new HttpRequest();

        $queries = DB::getQueryLog();
        foreach($queries as $q){
            SqlLog::create([
                'ip'=>$request->ip(),
                'user_id'=>Auth::user()->id,
                'name'=>Auth::user()->prenom.' '.Auth::user()->nom,
                'req_sql'=>$q['query'],
                'data'=>json_encode($q['bindings']),
                'time'=>$q['time'],
            ]);
        }
        } catch (\Throwable $th) {
            return "";
        }
    }
}
