feat(event): add slug field to events and update related resources and routes
This commit is contained in:
@@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['tenant_code', 'event_category_id', 'title', 'subtitle', 'description', 'location', 'exact_location', 'date_text', 'published_at', 'attachment_id'])]
|
||||
#[Fillable(['tenant_code', 'slug', 'event_category_id', 'title', 'subtitle', 'description', 'location', 'exact_location', 'date_text', 'published_at', 'attachment_id'])]
|
||||
class Event extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
@@ -17,6 +17,7 @@ class EventResource extends JsonResource
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'slug' => $this->slug,
|
||||
'title' => $this->title,
|
||||
'subtitle' => $this->subtitle,
|
||||
'description' => $this->description,
|
||||
|
||||
@@ -14,6 +14,7 @@ class PublicEventResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'slug' => $this->slug,
|
||||
'event_category_id' => $this->event_category_id,
|
||||
'title' => $this->title,
|
||||
'subtitle' => $this->subtitle,
|
||||
|
||||
@@ -6,9 +6,9 @@ use Illuminate\Support\Facades\Route;
|
||||
|
||||
require __DIR__.'/adminapp.php';
|
||||
|
||||
Route::prefix('tenants/{tenant:codigo}')->group(function (): void {
|
||||
Route::prefix('tenants/{tenant:codigo}')->scopeBindings()->group(function (): void {
|
||||
Route::get('events', [PublicEventController::class, 'index']);
|
||||
Route::get('events/{event}', [PublicEventController::class, 'show']);
|
||||
Route::get('events/{event:slug}', [PublicEventController::class, 'show']);
|
||||
});
|
||||
|
||||
Route::middleware('auth:sanctum')->post(
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('events', function (Blueprint $table): void {
|
||||
$table->string('slug')->nullable()->after('tenant_code');
|
||||
});
|
||||
|
||||
$usedSlugsByTenant = [];
|
||||
|
||||
foreach (DB::table('events')->orderBy('tenant_code')->orderBy('id')->get() as $event) {
|
||||
$baseSlug = Str::slug((string) $event->title);
|
||||
$baseSlug = $baseSlug !== '' ? mb_substr($baseSlug, 0, 240) : 'evento-'.$event->id;
|
||||
$slug = $baseSlug;
|
||||
$suffix = 2;
|
||||
|
||||
while (isset($usedSlugsByTenant[$event->tenant_code][$slug])) {
|
||||
$slug = $baseSlug.'-'.$suffix;
|
||||
$suffix++;
|
||||
}
|
||||
|
||||
DB::table('events')->where('id', $event->id)->update(['slug' => $slug]);
|
||||
$usedSlugsByTenant[$event->tenant_code][$slug] = true;
|
||||
}
|
||||
|
||||
Schema::table('events', function (Blueprint $table): void {
|
||||
$table->unique(['tenant_code', 'slug']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('events', function (Blueprint $table): void {
|
||||
$table->dropUnique(['tenant_code', 'slug']);
|
||||
$table->dropColumn('slug');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -115,6 +115,7 @@ class DesfilePuraTendenciaSeeder extends Seeder
|
||||
]);
|
||||
|
||||
$event = $tenant->events()->create([
|
||||
'slug' => 'desfile-pura-tendencia',
|
||||
'title' => 'Desfile Pura Tendencia',
|
||||
'location' => 'Salón Centro Recreativo Luz y Fuerza',
|
||||
'date_text' => '16 de Octubre 2026',
|
||||
|
||||
@@ -26,7 +26,7 @@ class MenuSeeder extends Seeder
|
||||
[
|
||||
'code' => 'event.detail',
|
||||
'label' => 'Detalle de evento',
|
||||
'route' => '/evento/:id',
|
||||
'route' => '/evento/:slug',
|
||||
],
|
||||
[
|
||||
'code' => 'product.category',
|
||||
|
||||
@@ -194,7 +194,7 @@ class MenuSeederTest extends TestCase
|
||||
foreach ([
|
||||
'event.index' => '/',
|
||||
'event.category' => '/eventos/categoria/:id',
|
||||
'event.detail' => '/evento/:id',
|
||||
'event.detail' => '/evento/:slug',
|
||||
] as $code => $route) {
|
||||
$menu = Menu::query()->where('code', $code)->firstOrFail();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user