refactor: use morph relations for auditable trait

This commit is contained in:
Roardom
2025-09-08 00:42:17 +00:00
parent 4e1d55d4fd
commit 58c86a5251
9 changed files with 74 additions and 21 deletions
+1 -3
View File
@@ -23,7 +23,6 @@ use App\Helpers\TorrentHelper;
use App\Helpers\TorrentTools;
use App\Http\Requests\StoreTorrentRequest;
use App\Http\Requests\UpdateTorrentRequest;
use App\Models\Audit;
use App\Models\Category;
use App\Models\Distributor;
use App\Models\History;
@@ -85,7 +84,7 @@ class TorrentController extends Controller
$user = $request->user();
$torrent = Torrent::withoutGlobalScope(ApprovedScope::class)
->with(['user', 'comments', 'category', 'type', 'resolution', 'subtitles', 'playlists', 'reports', 'featured', 'files'])
->with(['user', 'comments', 'category', 'type', 'resolution', 'subtitles', 'playlists', 'reports', 'featured', 'files', 'audits.user.group'])
->withCount([
'bookmarks',
'seeds' => fn ($query) => $query->where('active', '=', true)->where('visible', '=', true),
@@ -213,7 +212,6 @@ class TorrentController extends Controller
'mediaInfo' => $torrent->mediainfo !== null ? (new MediaInfo())->parse($torrent->mediainfo) : null,
'last_seed_activity' => History::where('torrent_id', '=', $torrent->id)->where('seeder', '=', 1)->latest('updated_at')->first(),
'playlists' => $user->playlists,
'audits' => Audit::with('user')->where('model_entry_id', '=', $torrent->id)->where('model_name', '=', 'Torrent')->latest()->get(),
'fileTree' => $fileTree,
]);
}
+2 -2
View File
@@ -76,8 +76,8 @@ class AuditLogSearch extends Component
get => Audit::query()
->with('user')
->when($this->username, fn ($query) => $query->whereRelation('user', 'username', '=', $this->username))
->when($this->modelName, fn ($query) => $query->where('model_name', '=', $this->modelName))
->when($this->modelId, fn ($query) => $query->where('model_entry_id', '=', $this->modelId))
->when($this->modelName, fn ($query) => $query->where('auditable_type', '=', 'App\\Models\\'.$this->modelName))
->when($this->modelId, fn ($query) => $query->where('auditable_id', '=', $this->modelId))
->when($this->action, fn ($query) => $query->where('action', '=', $this->action))
->when($this->record, fn ($query) => $query->where('record', 'LIKE', '%'.$this->record.'%'))
->latest()
+11 -3
View File
@@ -24,8 +24,8 @@ use Illuminate\Database\Eloquent\Model;
*
* @property int $id
* @property int|null $user_id
* @property string $model_name
* @property int $model_entry_id
* @property string $auditable_type
* @property int $auditable_id
* @property string $action
* @property mixed $record
* @property \Illuminate\Support\Carbon|null $created_at
@@ -47,7 +47,7 @@ class Audit extends Model
* @var list<string>
*/
protected $fillable = [
'user_id', 'model_name', 'model_entry_id', 'action', 'record',
'user_id', 'auditable_type', 'auditable_id', 'action', 'record',
];
/**
@@ -62,4 +62,12 @@ class Audit extends Model
'id' => User::SYSTEM_USER_ID,
]);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, $this>
*/
public function auditable(): \Illuminate\Database\Eloquent\Relations\MorphTo
{
return $this->morphTo();
}
}
+15 -6
View File
@@ -16,6 +16,7 @@ declare(strict_types=1);
namespace App\Traits;
use App\Models\Audit;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@@ -160,8 +161,8 @@ trait Auditable
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'auditable_type' => $model::class,
'auditable_id' => $model->{$model->getKeyName()},
'action' => 'create',
'record' => $data,
'created_at' => $now,
@@ -188,8 +189,8 @@ trait Auditable
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'auditable_type' => $model::class,
'auditable_id' => $model->{$model->getKeyName()},
'action' => 'update',
'record' => $data,
'created_at' => $now,
@@ -216,8 +217,8 @@ trait Auditable
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'auditable_type' => $model::class,
'auditable_id' => $model->{$model->getKeyName()},
'action' => 'delete',
'record' => $data,
'created_at' => $now,
@@ -225,4 +226,12 @@ trait Auditable
]);
}
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<Audit, $this>
*/
public function audits(): \Illuminate\Database\Eloquent\Relations\MorphMany
{
return $this->morphMany(Audit::class, 'auditable');
}
}
+2 -2
View File
@@ -38,8 +38,8 @@ class AuditFactory extends Factory
{
return [
'user_id' => User::factory(),
'model_name' => $this->faker->word(),
'model_entry_id' => $this->faker->randomDigitNotNull(),
'auditable_type' => $this->faker->word(),
'auditable_id' => $this->faker->randomDigitNotNull(),
'action' => $this->faker->word(),
'record' => json_encode(["key" => "value"], JSON_THROW_ON_ERROR),
];
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('audits', function (Blueprint $table): void {
$table->renameColumn('model_name', 'auditable_type');
$table->renameColumn('model_entry_id', 'auditable_id');
});
DB::table('audits')->update([
'auditable_type' => DB::raw("CONCAT('App\\\\Models\\\\', auditable_type)"),
]);
}
};
+3 -2
View File
@@ -146,8 +146,8 @@ DROP TABLE IF EXISTS `audits`;
CREATE TABLE `audits` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned DEFAULT NULL,
`model_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`model_entry_id` bigint unsigned NOT NULL,
`auditable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`auditable_id` bigint unsigned NOT NULL,
`action` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`record` json NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
@@ -3033,3 +3033,4 @@ INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (358,'2025_08_22_06
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (359,'2025_08_30_015125_create_torrent_reseeds_table',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (360,'2025_09_02_013312_add_color_icon_to_ticket_priorities_table',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (361,'2025_09_02_140036_add_anon_to_posts_table',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (362,'2025_09_08_000029_make_audits_morphable',1);
@@ -102,8 +102,8 @@
{{ strtoupper($audit->action) }}
</span>
</td>
<td>{{ $audit->model_name }}</td>
<td>{{ $audit->model_entry_id }}</td>
<td>{{ class_basename($audit->auditable_type) }}</td>
<td>{{ $audit->auditable_id }}</td>
<td>
<a href="{{ route('users.show', ['user' => $audit->user]) }}">
{{ $audit->user->username }}
@@ -23,7 +23,7 @@
</tr>
</thead>
<tbody>
@foreach ($audits->load(['user.group']) as $audit)
@foreach ($torrent->audits as $audit)
{{-- format-ignore-start --}}
@php $values = json_decode($audit->record, true); @endphp
{{-- format-ignore-end --}}