Skip to content

Added getSlugExistsQuery method (to allow overriding) & fixed unique slug generation

Compare
Choose a tag to compare
@voidgraphics voidgraphics released this 15 Mar 22:25
· 16 commits to master since this release

Fixed a problem with the slug generation if the slug already existed.

Previously, if the following slugs (foo, foo-1) existed in the database, the generated slug would end up being foo-1-1.
Now, it correctly generates foo-2.

Added getSlugExistsQuery method to allow overriding at the model level

Imagine your slug has a unique constraint based on the user_id. This means both user A and user B can have a post with the slug foo, but they can only have one post with that slug, each.

Previously, the package would hardcode the query that checks if the slug exists, and this use case couldn't be handled; the package had no way of knowing that it had to check the slugs for each user individually.

This can now be done by overriding the getSlugExistsQuery method:

protected function getSlugExistsQuery($whereKey, $slug)
{
    return static::where($whereKey, $slug)
        ->where('user_id', $this->user_id)
        ->withoutGlobalScopes();
}