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

Conversation

hendurhance
Copy link

PR Description:

This pull request adds the findBySlugOr() method to the HasSlug trait, providing a new feature that allows finding a model by its slug or executing a callback if the slug is not found.

Changes Made:

  • Added the findBySlugOr() method to the HasSlug trait.
  • Implemented the callback functionality to execute custom logic when the slug is not found.
  • Added a static method missingCallback exception to be thrown when a callback is missing
$slug = 'some-slug';

// Try to find a model with the given slug 'some-slug'.
// If not found, execute the callback and return its result.
$foundModel = Model::findBySlugOr($slug, ['*'], function ($slug) {
    // Your custom logic here, for example, create a new model with the given slug.
    $newModel = new Model(['slug' => $slug, 'name' => 'New Model']);
    $newModel->save();
    return $newModel;
});

Testing

  • Included tests to verify that the callback is executed correctly when the slug is not found.

@freekmurze
Copy link
Member

Thanks for your work on this.
To keep the package simple, I'm going to pass on this for now.

@freekmurze freekmurze closed this Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants