xt/laravel-external-id

Generate external id when saving Eloquent models

v1.0.1 2022-06-29 07:55 UTC

This package is auto-updated.

Last update: 2024-04-29 04:28:16 UTC


README

This package provides a trait that will generate a public usage id when saving any Eloquent model.

$model = new EloquentModel();
$model->save();

echo $model->external_id; // ouputs "activerecord-is-awesome"

Installation

You can install the package via composer:

composer require xt/laravel-external-id

Usage

Your Eloquent models should use the XT\ExternalId\HasExternalId trait and the XT\ExternalId\ExternalIdOptions class.

The trait contains an abstract method getExternalIdOptions() that you must implement yourself.

Your models' migrations should have a field to save the generated external id to.

Here's an example of how to implement the trait:

namespace App;

use XT\ExternalId\HasExternalId;
use XT\ExternalId\ExternalIdOptions;
use Illuminate\Database\Eloquent\Model;

class YourEloquentModel extends Model
{
    use HasExternalId;

    /**
     * Get the options for generating the slug.
     */
    public function getExternalIdOptions() : ExternalIdOptions
    {
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id');
    }
}

With its migration:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateYourEloquentModelTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('your_eloquent_models', function (Blueprint $table) {
            $table->increments('id');
            $table->string('external_id'); // Field name same as your `saveExternalIdTo`
            $table->string('name');
            $table->timestamps();
        });
    }
}

To get the record using external id, you can use findByExternalId method

YourEloquentModel::findByExternalId('<Your-ID>');

License

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