Drop-in model references.

Overview

Drop-in Model References

Build Status

This Laravel >=5.5 package provides a quick and simple way to add unique references to models that can be resolved via route model binding. You usually don't want to expose your primary keys to the client, and without a unique reference such as a username or slug, you can't quickly build a RESTful API. With this package you can give your models a unique reference and instantly start using it in your routes.

Here are a few short examples of what you can do:

// Simply add the binding to your route
Route::get('/api/customers/{ref}', function (Customer $customer) {
    //
});

// Then just use the model's reference in your request
GET /api/customers/cus_tKCulsB67hty

Installation

You can install this package via composer using this command:

composer require jameslkingsley/laravel-references

If you're using Laravel 5.5 or greater this package will be auto-discovered, however if you're using anything lower than 5.5 you will need to register it the old way:

Next, you must install the service provider in config/app.php:

'providers' => [
    ...
    Kingsley\References\ReferencesServiceProvider::class,
];

Now publish the config:

php artisan vendor:publish --provider="Kingsley\References\ReferencesServiceProvider"

This is the contents of the published config file:

return [
    /*
     * Name of the database table that
     * will store model references.
     */
    'table_name' => 'references',

    /*
     * Name of the route model binding.
     * Eg. /api/customers/{ref}
     */
    'binding_name' => 'ref',

    /*
     * Whether the reference hash should
     * prefix the shortened model type.
     * Eg. App\Customer -> cus_tKCulsB67hty
     */
    'prefix' => false,
];

Now just run the migrations!

php artisan migrate

Usage

Choose the model that you want to make referenceable. In this example I'll choose Customer. We'll import the trait and use it in the class.

namespace App;

use Kingsley\References\Referenceable;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use Referenceable;
}

Now setup your routes to use the binding. Whether you're using a controller or an anonymous function, just type-hint the model class in the arguments to resolve the reference.

Route::get('/api/customer/{ref}', function (Customer $customer) {
    return $customer;
});

When submitting AJAX requests from the client, you can use the ref attribute that is appended to the model.

ajax.get(`/api/customer/${customer.ref}`);

Prefixes

If you want to prefix references, just set the prefix option to true in the config. By default it will use the first three characters of the class' basename.

App\Customer -> cus_tKCulsB67hty

Alternatively you can explicitly set the prefix for the model by setting the following. Note that if you set it to null it won't have a prefix, even if your prefix config option is set to true.

class Customer extends Model
{
    use Referenceable;

    protected $referencePrefix = 'customer';
}
You might also like...
Update multiple Laravel Model records, each with it's own set of values, sending a single query to your database!

Laravel Mass Update Update multiple Laravel Model records, each with its own set of values, sending a single query to your database! Installation You

Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Laravel Quran is static Eloquent model for Quran.

Laravel Quran بِسْمِ ٱللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ Laravel Quran is static Eloquent model for Quran. The Quran has never changed and never will, bec

Save Model is a Laravel package that allows you to save data in the database in a new way.

Save Model is a Laravel package that allows you to save data in the database in a new way. No need to worry about $guarded and $fillable properties in the model anymore. Just relax an use Save Model package.

PHP 8 attribute to register Laravel model observers.

PHP 8 attribute to register Laravel model observers. Instead of defining observers inside service providers this package offers an alternative way to

A laravel package to attach uuid to model classes

Laravel Model UUID A simple package to generate model uuid for laravel models Installation Require the package using composer: composer require touhid

A simple laravel package to handle multiple key based model route binding

Laravel Model UUID A simple package to handle the multiple key/column based route model binding for laravel package Installation Require the package u

A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache.

Laravel OTP Introduction A package for Laravel One Time Password (OTP) generator and validation without Eloquent Model, since it done by Cache. The ca

Store and retrieve settings generally or for model objects in Laravel.
Store and retrieve settings generally or for model objects in Laravel.

Store and retrieve settings generally or for model objects in Laravel. Documentation You can find the detailed documentation here in Laravel Settings

Comments
  • How can I help?

    How can I help?

    I've wanted something like this but never sat down to do it myself. Rather than copying what you have and making my own, I want to contribute to this. Can you make a few issues or tasks that I could work on for you?

    question 
    opened by captbrogers 1
  • Add test for route model binding

    Add test for route model binding

    Wasn't able to get this to work, but according to the testing docs you just define a route and do something like $this->get('/the-route/<reference>').

    task 
    opened by jameslkingsley 1
Owner
James Kingsley
James Kingsley
A laravel package to generate model hashid based on model id column.

Laravel Model Hashid A package to generate model hash id from the model auto increment id for laravel models Installation Require the package using co

Touhidur Rahman 13 Jan 20, 2022
A package to filter laravel model based on query params or retrieved model collection

Laravel Filterable A package to filter laravel model based on query params or retrived model collection. Installation Require/Install the package usin

Touhidur Rahman 17 Jan 20, 2022
Laravel-model-mapper - Map your model attributes to class properties with ease.

Laravel Model-Property Mapper This package provides functionality to map your model attributes to local class properties with the same names. The pack

Michael Rubel 15 Oct 29, 2022
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023
Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.

Laravel Blade Sortable Demo Repo Installation You can install the package via composer: composer require asantibanez/laravel-blade-sortable After the

Andrés Santibáñez 370 Dec 23, 2022
Specify the tables that you don't want to drop during refreshing the database.

Custom Fresh Custom Fresh allows fine-grain control of migrations inside your Laravel project. You can choose which tables will not be dropped during

Mahmoud Mohamed Ramadan 7 Jul 25, 2022
Turn any Eloquent model into a list!

Listify Turn any Eloquent model into a list! Description Listify provides the capabilities for sorting and reordering a number of objects in a list. T

Travis Vignon 138 Nov 28, 2022
Easily create a revision history for any laravel model

Wouldn't it be nice to have a revision history for any model in your project, without having to do any work for it. By simply adding the RevisionableT

Venture Craft 2.4k Jan 6, 2023
laravel-model-validator

laravel-model-validator This is a simple validator. The validator can be created by command. The validator has all common table column constraint, eg:

null 6 May 22, 2022
Automatic Laravel model migrations.

Laravel Automatic Migrations Automatic Laravel model migrations. Instead of having to create and manage migration files, this package allows you to sp

null 38 Nov 11, 2022