Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findBySlugOr() method with callback functionality #268

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ $model = Article::findBySlug('my-article');

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

### Find models by slug or
For convenience, you can use the alias `findBySlugOr` to retrieve a model. The query will compare against the field passed to `saveSlugsTo` when defining the `SlugOptions`. If the model does not exist, the callable will be executed and the result will be returned.

```php
$model = Article::findBySlugOr('my-article', ['*'], function () {
return Article::create(['name' => 'my-article']);
});

// or
$model = Article::findBySlugOr('my-article', ['*'],fn () => throw new \Exception('Article not found'));
```


## Changelog

Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public static function invalidMaximumLength(): static
{
return new static('Maximum length should be greater than zero');
}

public static function missingCallback(): static
{
return new static('Could not determine which callback should be used');
}
}
15 changes: 15 additions & 0 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,19 @@ public static function findBySlug(string $slug, array $columns = ['*'])

return static::where($field, $slug)->first($columns);
}

public static function findBySlugOr(string $slug, array $columns = ['*'], ?callable $callback = null)
{
if (!is_callable($callback) && $callback !== null) {
throw InvalidOption::missingCallback();
}

$result = static::findBySlug($slug, $columns);

if (!$result && is_callable($callback)) {
return $callback($slug);
}

return $result;
}
}
19 changes: 19 additions & 0 deletions tests/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,22 @@ public function getSlugOptions(): SlugOptions

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

it('can use a callable using findBySlugOr 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::findBySlugOr('my-custom-url1', ['*'], function () {
return 'not found';
});

expect($savedModel)->toEqual('not found');
});
Loading