Here's a Spark migration example, using UUID as primary key:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSubscriptionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->id('user_id', true);
$table->string('name');
$table->id('stripe_id', true);
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
Schema::create('team_subscriptions', function (Blueprint $table) {
$table->id();
$table->id('team_id', true);
$table->string('name');
$table->string('stripe_id');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('subscriptions');
Schema::drop('team_subscriptions');
}
}
This is what you need to do on your config/database.php:
/*
|--------------------------------------------------------------------------
| ID column
|--------------------------------------------------------------------------
|
| Here is the configuration for the id column used in your migrations.
|
*/
'id' => [
'primary' => [
[
'method' => 'uuid',
'params' => [
'columnName' => 'id',
],
],
[
'method' => 'primary',
],
],
'foreign' => [
[
'method' => 'uuid',
'params' => [
'columnName' => 'id'
],
],
[
'method' => 'index',
],
],
],
Using this would be the equivalent of calling
$table->uuid('id')->primary();
Here's the same example, but using the default int type for id:
/*
|--------------------------------------------------------------------------
| ID column
|--------------------------------------------------------------------------
|
| Here is the configuration for the id column used in your migrations.
|
*/
'id' => [
'primary' => [
[
'method' => 'integer',
'params' => [
'columnName' => 'id',
'autoIncrement' => true
],
],
[
'method' => 'unsigned',
],
],
'foreign' => [
[
'method' => 'integer',
'params' => [
'columnName' => 'id',
'autoIncrement' => false
],
],
[
'method' => 'unsigned',
],
],
],
Some other examples of usage:
Schema::create('subscriptions', function ($table) {
$table->id(); /// creates a UUID 'id' primary key
$table->id('my_key'); /// creates a UUID 'my_key' primary key
$table->id('user_id', true); /// creates a UUID 'user_id' foreign key
$table->id('group_id', true)->nullable(); /// creates a UUID 'group_id' foreign key and make it nullable
});
// Those are usage examples, this is not a real migration.
//
// UUID is my use case, but if yours is varchar(10) or BIGINT or even INT, yeah, it's done.
//
// All over your app! Following the same pattern in all migrations of all packages you
// have installed in your app.
Here's the method on Blueprint, responsible for it:
/**
* Create a new id column on the table.
*
* @return \Illuminate\Support\Fluent
*/
public function id($columnName = null, $foreign = false)
{
$fluent = null;
foreach (config('database.id.'.($foreign ? 'foreign' : 'primary')) as $call)
{
if ($columnName && isset($call['params']['columnName']))
{
$call['params']['columnName'] = $columnName;
}
$fluent = call_user_func_array(
[$fluent ?: $this, $call['method']],
isset($call['params']) ? $call['params'] : []
);
}
return $fluent;
}