All Projects → spatie → Laravel Prefixed Ids

spatie / Laravel Prefixed Ids

Licence: mit
Friendly prefixed IDs for Laravel models

Projects that are alternatives of or similar to Laravel Prefixed Ids

Requent
A GraphQL like interface to map a request to eloquent query with data transformation for Laravel.
Stars: ✭ 78 (-11.36%)
Mutual labels:  api, eloquent, laravel
Lara Eye
Filter your Query\Builder using a structured query language
Stars: ✭ 39 (-55.68%)
Mutual labels:  api, eloquent, laravel
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-19.32%)
Mutual labels:  eloquent, laravel
Laravel Vue Starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 76 (-13.64%)
Mutual labels:  api, laravel
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (-11.36%)
Mutual labels:  eloquent, laravel
Laravel Optimistic Locking
Adds optimistic locking feature to eloquent models.
Stars: ✭ 71 (-19.32%)
Mutual labels:  eloquent, laravel
Eloquent Settings
Eloquent Settings allows you to bind key-value pairs to any Laravel Eloquent model. It supports even casting for boolean, float or integer types.
Stars: ✭ 71 (-19.32%)
Mutual labels:  eloquent, laravel
Laravel Lucene Search
Laravel 4.2, 5.* package for full-text search over Eloquent models based on ZF2 Lucene.
Stars: ✭ 75 (-14.77%)
Mutual labels:  eloquent, laravel
Laravel Approvable
Easily add an approval process to any laravel model.
Stars: ✭ 79 (-10.23%)
Mutual labels:  eloquent, laravel
Flattable
It helps you manage de-normalized tables
Stars: ✭ 81 (-7.95%)
Mutual labels:  eloquent, laravel
Laravel Blog
Laravel 8.0 blog application with Vue.js, Homestead, Horizon, Telescope and Pusher
Stars: ✭ 1,248 (+1318.18%)
Mutual labels:  api, laravel
Dark Sky Api
PHP Library for the Dark Sky API.
Stars: ✭ 70 (-20.45%)
Mutual labels:  api, laravel
Laravel Api Boilerplate Jwt
A Laravel 5.8 API Boilerplate to create a ready-to-use REST API in seconds.
Stars: ✭ 1,155 (+1212.5%)
Mutual labels:  api, laravel
Elasticquent
Maps Laravel Eloquent models to Elasticsearch types
Stars: ✭ 1,172 (+1231.82%)
Mutual labels:  eloquent, laravel
Dreamfactory
DreamFactory API Management Platform
Stars: ✭ 1,148 (+1204.55%)
Mutual labels:  api, laravel
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (-26.14%)
Mutual labels:  eloquent, laravel
Laravel Woocommerce
WooCommerce Rest API for Laravel
Stars: ✭ 86 (-2.27%)
Mutual labels:  api, laravel
Laravel admin api
基于laravel5.5开发的基础后台管理脚手架, RBAC权限控制; 接口基于dingo/api和tymon/jwt, 可在此基础上完成你的laravel项目
Stars: ✭ 59 (-32.95%)
Mutual labels:  api, laravel
Eloquentfilter
An Eloquent Way To Filter Laravel Models And Their Relationships
Stars: ✭ 1,113 (+1164.77%)
Mutual labels:  eloquent, laravel
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (-10.23%)
Mutual labels:  eloquent, laravel

Friendly prefixed IDs for Laravel models

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Prefixing an id will help users to recognize what kind of id it is. Stripe does this by default: customer ids are prefixed with cus, secret keys in production are prefixed with sk_live_, secret keys of a testing environment with sk_test_ and so on....

This package can generate such friendly prefixed ids for Eloquent models. Here's how such generated ids could look like.

user_fj39fj3lsmxlsl
test_token_dvklms109dls

The package can retrieve the model for a given prefixed id.

// on a specific model
User::findByPrefixedId('user_fj39fj3lsmxlsl'); // returns a User model or `null`

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

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-prefixed-ids

Preparing your models

On each model that needs a prefixed id, you should use the Spatie\PrefixedIds\Models\Concerns\HasPrefixedId trait.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\PrefixedIds\Models\Concerns\HasPrefixedId;

class YourModel extends Model
{
    use HasPrefixedId;
}

Preparing the database

For each model that needs a prefixed id, you'll need to write a migration to add a prefixed_id column to its underlying table.

If you wish to use another attribute name, you should publish the config file (see below) and set the prefixed_id_attribute_name config value to the attribute name of your liking.

Schema::create('your_models_table', function (Blueprint $table) {
   $table->string('prefixed_id')->nullable()->unique();
});

Registering models with prefixed ids

To register your models, you should pass the desired prefix and the class name of your model to PrefixedIds::registerModels.

Spatie\PrefixedIds\PrefixedIds::registerModels([
    'your_prefix_' => YourModel::class,
    'another_prefix' => AnotherModel::class,
]);

Typically, you would put the code above in a service provider.

Publish the config file

Optionally, You can publish the config file with:

php artisan vendor:publish --provider="Spatie\PrefixedIds\PrefixedIdsServiceProvider" --tag="laravel-prefixed-ids-config"

This is the contents of the published config file:

return [
    /*
     * The attribute name used to store prefixed ids on a model
     */
    'prefixed_id_attribute_name' => 'prefixed_id',
];

Usage

When a model is created, it will automatically have a unique, prefixed id in the prefixed_id attribute.

$model = YourModel::create();
$model->prefixed_id // returns a random id like `your_model_fekjlmsme39dmMS`

Finding a specific model

You can find the model with a given prefix by calling findByPrefixedId on it.

YourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel`
YourModel::findByPrefixedId('non-existing-id'); // returns null

Finding across models

You can call find on Spatie\PrefixedIds\PrefixedIds to automatically get the right model for any given prefixed id.

$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`

Using the prefixed ids in your routes

To use the prefixed ids in your routes, you'll have to add the getRouteKeyName method to your model. It should return the name of the attribute that holds the prefixed id.

public function getRouteKeyName()
{
    return 'prefixed_id';
}

With this in place a route defined as...

Route::get('/api/your-models/{yourModel}', YourModelController::class)`

... can be invoked with an URL like /api/your-models/your_model_fekjlmsme39dmMS.

You'll find more info on route model binding in the Laravel docs.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This package is inspired by excid3/prefixed_ids

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].