69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\LogAction;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* @mixin IdeHelperQuote
|
|
*/
|
|
class Quote extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
public function request(): void
|
|
{
|
|
// Send the quote
|
|
// If success, add it to the transactions
|
|
|
|
Log::create([
|
|
'user_id' => auth()?->user()?->id,
|
|
'loggable_type' => self::class,
|
|
'loggable_id' => $this->id,
|
|
'action' => LogAction::REQUEST,
|
|
'content' => $this->quote,
|
|
'ip' => request()->ip(),
|
|
]);
|
|
}
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'quote',
|
|
];
|
|
|
|
public function logs(): MorphOne
|
|
{
|
|
return $this->morphOne(Log::class, 'loggable');
|
|
}
|
|
|
|
public static function boot(): void
|
|
{
|
|
parent::boot();
|
|
|
|
self::created(function (Quote $model) {
|
|
Log::create([
|
|
'user_id' => auth()?->user()?->id,
|
|
'loggable_type' => self::class,
|
|
'loggable_id' => $model->id,
|
|
'action' => LogAction::CREATE,
|
|
'content' => $model->quote,
|
|
'ip' => request()->ip(),
|
|
]);
|
|
});
|
|
|
|
self::deleted(function (Quote $model) {
|
|
Log::create([
|
|
'user_id' => auth()?->user()?->id,
|
|
'loggable_type' => self::class,
|
|
'loggable_id' => $model->id,
|
|
'action' => LogAction::DELETE,
|
|
'content' => $model->quote,
|
|
'ip' => request()->ip(),
|
|
]);
|
|
});
|
|
}
|
|
}
|