Files
portal-api/app/Support/GeneratesUniqueSlug.php
T
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

33 lines
777 B
PHP

<?php
namespace App\Support;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class GeneratesUniqueSlug
{
/**
* @param class-string<Model> $modelClass
*/
public static function forModel(string $modelClass, string $source, ?int $ignoreId = null): string
{
$baseSlug = Str::slug($source);
$slug = $baseSlug !== '' ? $baseSlug : 'item';
$baseSlug = $slug;
$counter = 1;
while (
$modelClass::withTrashed()
->where('slug', $slug)
->when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId))
->exists()
) {
$slug = "{$baseSlug}-{$counter}";
$counter++;
}
return $slug;
}
}