1. Go to this page and download the library: Download xt/laravel-external-id library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xt / laravel-external-id example snippets
$model = new EloquentModel();
$model->save();
echo $model->external_id; // ouputs "activerecord-is-awesome"
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');
}
}
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();
});
}
}
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_<generated_id>
// 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);
});
}
}
YourEloquentModel::findByExternalId('<Your-ID>');
YourEloquentModel::findByExternalId('<Your-ID>', ['column1', 'column2']);
OR
YourEloquentModel::findByExternalIdOrFail('<Your-ID>');
YourEloquentModel::findByExternalIdOrFail('<Your-ID>', ['column1', 'column2']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.