Laravel Prefixed IDs is a package by Spatie that automatically adds a friendly prefixed ID to Laravel models. As Freek Van der Herten describes in his introductory post about this package, it provides human-readable prefixes to otherwise random IDs. For example, you might give a token ID that contains a friendly prefix:
1token_sandbox_FKdleMEKfiemsiDSMEODL
The token_sandbox
prefix could indicate that it's a token for your sandbox environment, which would otherwise look like a random ID.
Here's an example model from Freek's article that illustrates how you use this package to indicate the model has a prefixed column:
1namespace App\Models;2 3use Illuminate\Database\Eloquent\Model;4use Spatie\PrefixedIds\Models\Concerns\HasPrefixedId;5 6class YourModel extends Model7{8 use HasPrefixedId;9}
If you follow the installation instructions, you'll need a table column for your model that contains the prefixed id, which defaults to prefixed_id
.
Here are a few examples of how you might use this package with a model containing a prefixed ID:
1$model = ApiToken::create();2$model->prefixed_id3 4ApiToken::findByPrefixedId('token_sandbox_fekjlmsme39dmMS'); // returns model5ApiToken::findByPrefixedId('invalid-id'); // null
You can learn more about this package, get full installation instructions, and view the source code on GitHub.
0 comments:
Post a Comment
Thanks