Search by

invoate / console-commands

OliverLumby

A collection of useful Artisan console commands for Laravel.

Package info

github.com/invoate/console-commands

pkg:composer/invoate/console-commands

Statistics

Installs: 147

Dependents: 0

Suggesters: 0

Stars: 3

1.0.1 2023-03-07 18:29 UTC

This package is auto-updated.

Last update: 2026-10-24 04:55:50 UTC


README

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');
    //..
});

Foreign keys can be set to cascade on delete with the --cascade flag.

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

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

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

A soft delete column can be added with the --soft-deletes flag.

php artisan make:pivot User Team --soft-deletes
Schema::create('teams_users', function (Blueprint $table) {
    //..
    $table->timestamps();
    $table->softDeletes();
});

Additional columns can be created using the --columns flag, formatted as column_name:column_type.

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

A pivot Eloquent model can be generated with the --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.

Optionally 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) {
    //
});

Trait Make Command

The Trait Make Command generates a new trait class.

php artisan make:trait MyTrait

This will create app/Traits/MyTrait.php:

<?php

namespace App\Traits;

trait MyTrait
{
    //
}

Sub-namespaces are supported by passing a path:

php artisan make:trait Auth/HasPermissions

This creates app/Traits/Auth/HasPermissions.php with the namespace App\Traits\Auth.

The default namespace (App\Traits) can be changed by publishing the config file:

php artisan vendor:publish --tag="console-commands-config"

Refresh

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

php artisan refresh

A specific seeder class can be used with the --class option.

php artisan refresh --class=UsersTableSeeder

Testing

composer test

Credits

License

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