Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josephxanderson/1d488583043ee654f6e00b9c656fd45c to your computer and use it in GitHub Desktop.
Save josephxanderson/1d488583043ee654f6e00b9c656fd45c to your computer and use it in GitHub Desktop.
[See How to Implement.txt below] This Gist shows how to implement a Client model observer for Laravel Passport so that you don't have to be stuck with that weird autoincrementing Client ID.
<?php
namespace App\Observers;
use Laravel\Passport\Client;
class ClientObserver
{
/**
* Listen to the Client creating event.
*
* @param \Laravel\Passport\Client $client
* @return void
*/
public function creating(Client $client)
{
// Set the model ID.
$client->setAttribute('id', Randomizer::createNumericId()); // This is where you will need to update this code to create the type of random, numeric ID you want.
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Client;
use Laravel\Passport\Passport;
use App\Observers\ClientObserver;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Register model observers.
Client::observe(ClientObserver::class);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
Passport::ignoreMigrations();
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_clients', function (Blueprint $table) {
$table->bigInteger('id')->unsigned();
$table->integer('user_id')->index()->nullable();
$table->string('name');
$table->string('secret', 100);
$table->text('redirect');
$table->boolean('personal_access_client');
$table->boolean('password_client');
$table->boolean('revoked');
$table->timestamps();
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthPersonalAccessClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_personal_access_clients', function (Blueprint $table) {
$table->bigInteger('id');
$table->bigInteger('client_id')->index();
$table->timestamps();
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthAccessTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->integer('user_id')->index()->nullable();
$table->bigInteger('client_id');
$table->string('name')->nullable();
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->timestamps();
$table->dateTime('expires_at')->nullable();
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthAuthCodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_auth_codes', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->integer('user_id');
$table->bigInteger('client_id');
$table->text('scopes')->nullable();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthRefreshTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_refresh_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('access_token_id', 100)->index();
$table->boolean('revoked');
$table->dateTime('expires_at')->nullable();
});
}
}
This Gist provides an example of how to implement a Client Model Observer for Laravel Passport.
This Observer will allow you to intercept the creation of a `Laravel\Passport\Client` model and set a custom id for it.
However, to implement this, you will need to handle the migrations yourself. In my case, the custom ID will be a random, large integer. So the database migrations set the `id` field to a type of `bigInteger`.
Take a look at this complete Gist carefully and implement it yourself. It's very easy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment