PHPackages                             xt/laravel-external-id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. xt/laravel-external-id

ActiveLibrary[Database &amp; ORM](/categories/database)

xt/laravel-external-id
======================

Generate external id when saving Eloquent models

v1.0.3(8mo ago)0122↓50%MITPHPPHP ^7.4|^8.0|^8.1|^8.2|^8.3|^8.4

Since Jan 2Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/hirenreshamwala/laravel-external-id)[ Packagist](https://packagist.org/packages/xt/laravel-external-id)[ RSS](/packages/xt-laravel-external-id/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

Generate public usage id when saving Eloquent models
====================================================

[](#generate-public-usage-id-when-saving-eloquent-models)

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

[](#installation)

You can install the package via composer:

```
composer require xt/laravel-external-id
```

Usage
-----

[](#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();
        });
    }
}
```

You can also specify prefix, length, numeric id only, time based id or create your own custom id

```
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
    {
        // generate alpha numeric id of particular length
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->setLength(16);

        // Generate id with prefix
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->setPrefix('order_'); // Output: order_

        // Generate numeric id of particular length
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->setLength(16)
            ->setIsNumberOnly(true); //Optional

        // Generate incremental id
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->incremental(1); // increment start from 1. Default value is: 1

        // Generate unix timestamp based id
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->setIsTimeBase(true); // Output: 16628769732187896

        // You can create your own id generation logic
        return ExternalIdOptions::create()
            ->saveExternalIdTo('external_id')
            ->customIdScope(function () {
                return getRandomString(6, false).getRandomNumber(6);
            });
    }
}
```

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

```
YourEloquentModel::findByExternalId('');

YourEloquentModel::findByExternalId('', ['column1', 'column2']);

OR

YourEloquentModel::findByExternalIdOrFail('');

YourEloquentModel::findByExternalIdOrFail('', ['column1', 'column2']);
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance62

Regular maintenance activity

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~573 days

Total

4

Last Release

242d ago

PHP version history (4 changes)v1.0PHP ^7.4

v1.0.1PHP ^7.4|^8.0|^8.1

v1.0.2PHP ^7.4|^8.0|^8.1|^8.2|^8.3

v1.0.3PHP ^7.4|^8.0|^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c3a9aec52ca2e39b71f5a3aaacec11c7352fa97bb52b04293198e1831982740?d=identicon)[hirenreshamwala](/maintainers/hirenreshamwala)

---

Top Contributors

[![hirenreshamwala](https://avatars.githubusercontent.com/u/1775052?v=4)](https://github.com/hirenreshamwala "hirenreshamwala (10 commits)")

---

Tags

eloquentxtlaravel-external-id

### Embed Badge

![Health badge](/badges/xt-laravel-external-id/health.svg)

```
[![Health](https://phpackages.com/badges/xt-laravel-external-id/health.svg)](https://phpackages.com/packages/xt-laravel-external-id)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/rememberable

Query caching for Laravel

1.1k5.2M13](/packages/watson-rememberable)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
