Skip to content

scopeWithUniqueSlugConstraints

Shipu Ahamed edited this page Sep 3, 2016 · 1 revision

scopeWithUniqueSlugConstraints

/**
 * @param \Illuminate\Database\Eloquent\Builder $query
 * @param \Illuminate\Database\Eloquent\Model $model
 * @param string $attribute
 * @param array $config
 * @param string $slug
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeWithUniqueSlugConstraints(Builder $query, Model $model, $attribute, $config, $slug)
{
    ...
}

If this scope exists on your model, then it will also be applied to the query used to determine if a given slug is unique. The arguments passed to the scope are:

  • $model -- the object being slugged
  • $attribute -- the slug field being generated,
  • $config -- the configuration array for the given model and attribute
  • $slug -- the "base" slug (before any unique suffixes are applied)

Feel free to use these values anyway you like in your query scope. As an example, look at tests/Models/PostWithUniqueSlugConstraints.php where we generate a slug for a post from it's title, but the slug is scoped to the author. So Bob can have a post with the same title as Pam's post, but both will have the same slug.

Clone this wiki locally