You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class TvLog extends Model {
|
|
use HasFactory;
|
|
|
|
protected $table = 'tv_logs';
|
|
protected $hidden = ['ik_cust_id', 'ik_address_id'];
|
|
protected $casts = ['from'=>'object', 'to'=>'object'];
|
|
|
|
const TYPES = ['create', 'update', 'update-excel'];
|
|
|
|
public static function historyCreate(User $user, int $tvFk, Tv $newTv) { self::saveHistory('create', $user, $tvFk, null, $newTv); }
|
|
public static function historyUpdate(User $user, int $tvFk, Tv $oldTv, Tv $newTv) { self::saveHistory('update', $user, $tvFk, $oldTv, $newTv); }
|
|
public static function historyUpdateExcel(User $user, int $tvFk, Tv $oldTv, Tv $newTv) { self::saveHistory('update-excel', $user, $tvFk, $oldTv, $newTv); }
|
|
|
|
private static function saveHistory(String $type, ?User $user, int $tvFk, ?Tv $oldTv, ?Tv $newTv) {
|
|
if(!in_array($type, self::TYPES)) throw new \Exception("Type '$type' No Valid");
|
|
$tvLog = new TvLog();
|
|
$tvLog->tv_fk = $tvFk;
|
|
$tvLog->type = $type;
|
|
if($oldTv) {
|
|
$oldTv = $oldTv->toArray();
|
|
unset($oldTv['id']);
|
|
$tvLog->from =$oldTv;
|
|
}
|
|
if($newTv) {
|
|
$newTv = $newTv->toArray();
|
|
unset($newTv['id']);
|
|
$tvLog->to = $newTv;
|
|
}
|
|
if($user) { $tvLog->user_fk = $user->id; }
|
|
$tvLog->save();
|
|
}
|
|
}
|