Skip to content
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ The package can retrieve the model for a given prefixed id.
```php
// on a specific model
User::findByPrefixedId('user_fj39fj3lsmxlsl'); // returns a User model or `null`
User::findByPrefixedIdOrFail('user_fj39fj3lsmxlsl'); // returns a User model or throws `NoPrefixedModelFound`

// automatically determine the model of a given prefixed id
$user = PrefixedIds::getModel('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;
$user = PrefixedIds::getModelClass('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;
```

## Support us
Expand Down Expand Up @@ -84,6 +85,7 @@ Typically, you would put the code above in a service provider.
### Publish the config file

Optionally, You can publish the config file with:

```bash
php artisan vendor:publish --provider="Spatie\PrefixedIds\PrefixedIdsServiceProvider" --tag="prefixed-ids-config"
```
Expand Down Expand Up @@ -115,6 +117,7 @@ You can find the model with a given prefix by calling `findByPrefixedId` on it.
```php
YourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel`
YourModel::findByPrefixedId('non-existing-id'); // returns null
YourModel::findByPrefixedIdOrFail('non-existing-id'); // throws `NoPrefixedModelFound`
```

### Finding across models
Expand All @@ -124,6 +127,7 @@ You can call `find` on `Spatie\PrefixedIds\PrefixedIds` to automatically get the
```php
$yourModel = Spatie\PrefixedIds\PrefixedIds::find('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel` or `null`
$otherModel = Spatie\PrefixedIds\PrefixedIds::find('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or `null`
$otherModel = Spatie\PrefixedIds\PrefixedIds::findOrFail('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or throws `NoPrefixedModelFound`
```

## Using the prefixed ids in your routes
Expand Down
25 changes: 25 additions & 0 deletions src/Exceptions/NoPrefixedModelFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\PrefixedIds\Exceptions;

use Exception;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;

class NoPrefixedModelFound extends Exception implements ProvidesSolution
{
public static function make(string $prefixedId)
{
return new static("Could not find a prefixed model `{$prefixedId}`");
}

public function getSolution(): Solution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

{
return BaseSolution::create('Make sure the prefix of the model exists')
->setSolutionDescription("You should make sure your model has a valid `prefixed_id`")
->setDocumentationLinks([
'Documentation' => 'https://github.com/spatie/laravel-prefixed-ids#usage',
]);
}
}
10 changes: 10 additions & 0 deletions src/Models/Concerns/HasPrefixedId.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\PrefixedIds\Exceptions\NoPrefixConfiguredForModel;
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;
use Spatie\PrefixedIds\PrefixedIds;

trait HasPrefixedId
Expand Down Expand Up @@ -32,6 +33,15 @@ public static function findByPrefixedId(string $prefixedId): ?Model
return static::firstWhere($attributeName, $prefixedId);
}

public static function findByPrefixedIdOrFail(string $prefixedId): Model
{
if (is_null($model = static::findByPrefixedId($prefixedId))) {
throw NoPrefixedModelFound::make($prefixedId);
}

return $model;
}

protected function getIdPrefix(): string
{
$prefix = PrefixedIds::getPrefixForModel(static::class);
Expand Down
10 changes: 10 additions & 0 deletions src/PrefixedIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\PrefixedIds;

use Illuminate\Database\Eloquent\Model;
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;

class PrefixedIds
{
Expand Down Expand Up @@ -41,6 +42,15 @@ public static function find(string $prefixedId): ?Model
return $modelClass::findByPrefixedId($prefixedId);
}

public static function findOrFail(string $prefixedId): ?Model
{
if (! $modelClass = static::getModelClass($prefixedId)) {
throw NoPrefixedModelFound::make($prefixedId);
}

return $modelClass::findByPrefixedIdOrFail($prefixedId);
}

public static function getModelClass(string $prefixedId): ?string
{
foreach (static::$registeredModels as $prefix => $modelClass) {
Expand Down
41 changes: 41 additions & 0 deletions tests/PrefixedIdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\PrefixedIds\Tests;

use Spatie\PrefixedIds\Exceptions\NoPrefixConfiguredForModel;
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;
use Spatie\PrefixedIds\PrefixedIds;
use Spatie\PrefixedIds\Tests\TestClasses\Models\OtherTestModel;
use Spatie\PrefixedIds\Tests\TestClasses\Models\TestModel;
Expand Down Expand Up @@ -78,4 +79,44 @@ public function it_can_find_the_right_model_for_the_given_prefixed_id()
$nonExistingModel = PrefixedIds::find('non-existing-id');
$this->assertNull($nonExistingModel);
}

/** @test */
public function a_model_can_find_or_fail_a_record_with_the_given_prefixed_id()
{
$testModel = TestModel::create();

$foundModel = TestModel::findByPrefixedIdOrFail($testModel->prefixed_id);
$this->assertEquals($testModel->id, $foundModel->id);
}

/** @test */
public function it_throws_exception_on_invalid_prefixed_id()
{
$this->expectException(NoPrefixedModelFound::class);

TestModel::findByPrefixedIdOrFail('non_existing');
}

// /** @test */
public function it_can_find_or_fail_the_right_model_for_the_given_prefixed_id()
{
$testModel = TestModel::create();
$otherTestModel = OtherTestModel::create();

$foundModel = PrefixedIds::findOrFail($testModel->prefixed_id);
$this->assertInstanceOf(TestModel::class, $foundModel);
$this->assertEquals($testModel->id, $foundModel->id);

$otherFoundModel = PrefixedIds::findOrFail($otherTestModel->prefixed_id);
$this->assertInstanceOf(OtherTestModel::class, $otherFoundModel);
$this->assertEquals($testModel->id, $otherFoundModel->id);
}

// /** @test */
public function it_throws_exception_on_invalid_given_model_prefixed_id()
{
$this->expectException(NoPrefixedModelFound::class);

PrefixedIds::findOrFail('non-existing-id');
}
}