mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 05:51:09 -05:00
feat: add Company model and TrackedModel trait; update Park model to use Company as owner
This commit is contained in:
34
app/Models/Company.php
Normal file
34
app/Models/Company.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\TrackedModel;
|
||||
|
||||
class Company extends Model
|
||||
{
|
||||
use HasFactory, TrackedModel;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
'website',
|
||||
'founded_year',
|
||||
'headquarters',
|
||||
'status'
|
||||
];
|
||||
|
||||
public function parks()
|
||||
{
|
||||
return $this->hasMany(Park::class, 'owner_id');
|
||||
}
|
||||
|
||||
public function updateStatistics()
|
||||
{
|
||||
// Implementation matching Django's Company.update_statistics()
|
||||
$this->loadCount('parks');
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,13 @@ use App\Enums\ParkStatus;
|
||||
use App\Traits\HasLocation;
|
||||
use App\Traits\HasSlugHistory;
|
||||
use App\Traits\HasParkStatistics;
|
||||
use App\Traits\TrackedModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use App\Models\Company;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class Park extends Model
|
||||
@@ -84,9 +86,9 @@ class Park extends Model
|
||||
/**
|
||||
* Get the operator that owns the park.
|
||||
*/
|
||||
public function operator(): BelongsTo
|
||||
public function owner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Operator::class);
|
||||
return $this->belongsTo(Company::class, 'owner_id');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
42
app/Traits/TrackedModel.php
Normal file
42
app/Traits/TrackedModel.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
trait TrackedModel {
|
||||
protected static function bootTrackedModel() {
|
||||
static::created(function (Model $model) {
|
||||
static::logChange($model, 'created');
|
||||
});
|
||||
|
||||
static::updated(function (Model $model) {
|
||||
static::logChange($model, 'updated');
|
||||
});
|
||||
|
||||
static::deleted(function (Model $model) {
|
||||
static::logChange($model, 'deleted');
|
||||
});
|
||||
}
|
||||
|
||||
protected static function logChange(Model $model, string $action) {
|
||||
$changes = $action === 'updated'
|
||||
? [
|
||||
'old' => $model->getOriginal(),
|
||||
'new' => $model->getDirty()
|
||||
]
|
||||
: $model->getAttributes();
|
||||
|
||||
DB::table('model_history')->insert([
|
||||
'model_type' => get_class($model),
|
||||
'model_id' => $model->getKey(),
|
||||
'user_id' => Auth::check() ? Auth::id() : null,
|
||||
'action' => $action,
|
||||
'changes' => json_encode($changes),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user