35 lines
900 B
PHP
35 lines
900 B
PHP
<?php
|
|
|
|
namespace App\Actions\Events;
|
|
|
|
use App\Models\Event;
|
|
use Illuminate\Support\Str;
|
|
|
|
class UpdateEventAction
|
|
{
|
|
public function execute(Event $event, array $data): Event
|
|
{
|
|
return \Illuminate\Support\Facades\DB::transaction(function () use ($event, $data) {
|
|
if (isset($data['slug']) && $data['slug'] !== $event->slug) {
|
|
$data['slug'] = $this->generateUniqueSlug($data['slug'], $event->id);
|
|
}
|
|
$event->update($data);
|
|
|
|
return $event->fresh();
|
|
});
|
|
}
|
|
|
|
protected function generateUniqueSlug(string $base, int $excludeId): string
|
|
{
|
|
$slug = Str::slug($base);
|
|
$original = $slug;
|
|
$count = 0;
|
|
while (Event::where('slug', $slug)->where('id', '!=', $excludeId)->exists()) {
|
|
$count++;
|
|
$slug = $original.'-'.$count;
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
}
|