Skip to content

invoate/console-commands

Repository files navigation

Invoate Console Commands

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

These are a few console commands packaged together so they don't have to be recreated in new projects.

Installation

You can install the package via composer:

composer require invoate/console-commands

Usage

Pivot Make Command

The Pivot Make Command generates a migration for a pivot table. Table names can be passed in any order, the command will alphabetise them.

php artisan make:pivot table1 table2

The table1 and table2 arguments can be existing Eloquent models or table names. Table names for Eloquent models will be automatically resolved.

php artisan make:pivot User Team
Schema::create('teams_users', function (Blueprint $table) {
    $table->foreignId('team_id')->constrained();
    $table->foreignId('user_id')->constrained();
    //..
});

By default the command generates foreign keys, this can be disabled with the --without-foreign-keys flag.

php artisan make:pivot User Team --without-foreign-keys
Schema::create('teams_users', function (Blueprint $table) {
    $table->integer('team_id');
    $table->integer('user_id');
    //..
});

Timestamp columns are also generated by default, the --without-timestamps flag will disable these.

php artisan make:pivot User Team --without-timestamps

Additional columns can be created using the --columns flag, they must be formatted in the column_name:column_type format.

php artisan make:pivot User Team --columns string:role,timestamp:expires_at
Schema::create('teams_users', function (Blueprint $table) {
    //..
    $table->string('role');
    $table->timestamp('expires_at');
    //..
});

A pivot Eloquent model can be generated with the --with-model flag.

php artisan make:pivot User Team --model

# INFO  Migration [database/migrations/2023_02_20_153354_create_teams_users_table.php] created successfully.
# INFO  Model [app/Models/TeamUser.php] created successfully.

Optionaly an id column can be generated using the --with-id flag. By default this will generate a $table->id() column, passing another Laravel supported column type with e.g. --id-type ulid will change this.

php artisan make:pivot User Team --with-id --id-type ulid
Schema::create('teams_users', function (Blueprint $table) {
    $table->ulid();
    //..
});

If you do not wish to generate any columns for the pivot table you can use the --without-columns flag.

php artisan make:pivot User Team --without-columns
Schema::create('teams_users', function (Blueprint $table) {
    //
});

Refresh

The RefreshCommand is a simple wrapper for php artisan migrate:fresh --seed.

php artisan refresh

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.