Skip to content
On this page

Migrations

The migrations key adds additional tables, and allows to override automatic pivot tables.

Define migrations using the table name as key, and a list of columns directly under the name. These are passed as-it-is to the migration, using the same column notation you're familiar with.

For example, we will declare the user_actions table like it was a Model. All method and arguments will be taken verbatim to be used in the migration.

yaml
migrations:
  user_actions:
    id: uuid primary
    username: string
    action: string
    route: string
    created_at: timestamp useCurrent
1
2
3
4
5
6
7
php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_actions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('username');
            $table->string('action');
            $table->string('route');
            $table->timestamp('created_at')->useCurrent();
        });
    }

    /**
     * Reverse the migrations.
     *
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_actions');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

Using foreignIdFor

Laravel includes the foreignIdFor method that automatically generates a foreign column reference to the model by checking its primary key. This is what Larawiz uses to create relations.

You can also use this method manually by setting the model name as key, and the foreignIdFor as value. The value accepts the column name as first argument.

yaml
migrations:
  user_actions:
    id:
    User: foreignIdFor
    Action: foreignIdFor:movement_id
    name: string
1
2
3
4
5
6
php
Schema::create('user_actions', function (Blueprint $table) {
    $table->id();
    $table->foreignIdFor(User::class);
    $table->foreignIdFor(Action::class, 'movement_id');
    $table->string('name');
});
1
2
3
4
5
6

Overriding pivot tables

When using automatic pivot tables or polymorphic pivot tables, you can override the tables by just issuing the table name with your own columns. Larawiz will automatically use your migration instead of the automatic pivot table.

For example, we can override the card_player table to add some information about the card, like an aliased name the player may have for it. Then, we can use withPivot with the name of the additional pivot name to include when retrieving the relation through the pivot record.

These are your keys

When overriding pivot tables, Larawiz hands-off all checks to you, so ensure your pivot tables contain the necessary relation columns.

yaml
models:  
  Card:
    name: string
    player: belongsToMany:Player

  Player:
    name: string
    email: string
    password: string
    cards: belongsToMany:Card withPivot:alias

migrations:
  card_player:
    id: ~
    printer: uuid
    Player: foreignIdFor
    Card: foreignIdFor    
    alias: string nullable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
php
class Card extends Model
{
    public function players()
    {
        return $this->belongsToMany(Player::class);
    }
}

class Player extends Model
{
    public function cards()
    {
        return $this->belongsToMany(Card::class)->withPivot(['alias']);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
php
Schema::create('cards', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

Schema::create('players', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email');
    $table->string('password');
    $table->timestamps();
});

Schema::create('card_player', function (Blueprint $table) {
    $table->id();
    $table->uuid('printer');
    $table->unsignedBigInteger('card_id');
    $table->unsignedBigInteger('player_id');
    $table->string('alias')->nullable();
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

The same concept applies for polymorphic pivot tables. In the next example, the automatic table is called routables, so with adding the migration manually you can customize the table, like adding a column to point out the difficulty for the attached Car or Bus model.

yaml
models:  
  Car:
    model: string
    routes: morphToMany:Tag,routable withPivot:difficulty

  Bus:
    model: string
    routes: morphToMany:Tag,routable withPivot:difficulty

  Route:
    name: string
    cars: morphToMany:Car,routable
    buses: morphToMany:Bus,routable

migrations:
  routables:
    id: ~
    route_id: unsignedBigInteger
    routable: morphs
    difficulty: string default:low
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
php
class Car extends Model
{
    public function routes()
    {
        return $this->morphToMany(Route::class, 'routable')
                    ->withPivot(['difficulty']);
    }
}

class Bus extends Model
{
    public function routes()
    {
        return $this->morphToMany(Route::class, 'routable')
                    ->withPivot(['difficulty']);
    }
}

class Route extends Model
{
    public function cars()
    {
        return $this->morphToMany(Car::class);
    }
    
    public function buses()
    {
        return $this->morphToMany(Bus::class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
php
Schema::create('cars', function (Blueprint $table) {
    $table->id();
    $table->string('model');
    $table->timestamps();
});

Schema::create('buses', function (Blueprint $table) {
    $table->id();
    $table->string('model');
    $table->timestamps();
});

Schema::create('routes', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

Schema::create('routables', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('route_id');
    $table->morphs('routable');
    $table->string('difficulty')->default('low');
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Pivots override only

If you declare the Pivot Model, and create a migration for it, you will get an error. Only automatic pivot tables can be overridden with a migration.

In other words, you may create any migrations as long it doesn't collide with a model you have declared previously.

Included migrations

Larawiz includes Laravel default migrations which are key for all fresh installations:

  • failed_jobs, to handle failed jobs from the queue,
  • password_resets, to handle users password resets, and,
  • personal_access_tokens, to handle api-style authentication through Laravel Sanctum.

These will be added in your scaffolded App automatically. You can disable each of them by issuing false:

yaml
migrations:
  failed_jobs: false
  password_resets: false
  personal_access_tokens: false
1
2
3
4

They are as it is

The included migrations cannot be overridden with your own. You may do it after your application is scaffolded.