Skip to content

Commit

Permalink
Merge pull request #256 from IsraelOrtuno/main
Browse files Browse the repository at this point in the history
feat: add findBySlug alias
  • Loading branch information
freekmurze authored May 29, 2023
2 parents 9bab992 + d691887 commit ddfb38d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ class YourEloquentModel extends Model
}
```

### Find models by slug

For convenience, you can use the alias `findBySlug` to retrieve a model. The query will compare against the field passed to `saveSlugsTo` when defining the `SlugOptions`.

```php
$model = Article::findBySlug('my-article');
```

`findBySlug` also accepts a second parameter `$columns` just like the default Eloquent `find` method.


## Changelog

Expand Down
13 changes: 13 additions & 0 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\Sluggable\Exceptions\InvalidOption;
use Spatie\Sluggable\Tests\TestSupport\TranslatableModel;

trait HasSlug
{
Expand Down Expand Up @@ -189,4 +190,16 @@ protected function generateSubstring($slugSourceString)

return substr($slugSourceString, 0, $this->slugOptions->maximumLength);
}

public static function findBySlug(string $slug, array $columns = ['*'])
{
$modelInstance = new static;
$field = $modelInstance->getSlugOptions()->slugField;

$field = in_array(HasTranslatableSlug::class, class_uses_recursive(static::class))
? "{$field}->{$modelInstance->getLocale()}"
: $field;

return static::where($field, $slug)->first($columns);
}
}
16 changes: 16 additions & 0 deletions tests/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,19 @@ public function getSlugOptions(): SlugOptions
expect($model->url)->toEqual('this-is-a-test');
expect($replica->url)->toEqual('this-is-a-test-2');
});

it('can find models using findBySlug alias', function () {
$model = new class () extends TestModel {
public function getSlugOptions(): SlugOptions
{
return parent::getSlugOptions()->saveSlugsTo('url');
}
};

$model->name = 'my custom url';
$model->save();

$savedModel = $model::findBySlug('my-custom-url');

expect($savedModel->id)->toEqual($model->id);
});
16 changes: 16 additions & 0 deletions tests/HasTranslatableSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,19 @@ function (TestModel $testModel, TranslatableModel $translatableModel) use ($pare

$response->assertStatus(200);
});

it('can find models using findBySlug alias', function () {
$model = new class () extends TranslatableModel {
public function getSlugOptions(): SlugOptions
{
return parent::getSlugOptions()->saveSlugsTo('slug');
}
};

$model->name = 'my custom url';
$model->save();

$savedModel = $model::findBySlug('my-custom-url');

expect($savedModel->id)->toEqual($model->id);
});

0 comments on commit ddfb38d

Please sign in to comment.