erikaraujo/laravel-stubs

Opinionated Laravel stubs with resources

1.0.1 2021-04-16 05:00 UTC

This package is auto-updated.

Last update: 2024-12-16 13:40:47 UTC


README

Main stub changes:

  • controllers don't extend a base Controller;
  • depending on the parameters passed, this also adds resources for controllers;
  • migrations don't have a down function;
  • model has with and fillable by default;
  • docblocks have been removed;

Installation

Install the package via composer:

composer require erikaraujo/laravel-stubs --dev

If you want to keep your stubs up to date with every update, add this composer hook to your composer.json file:

"scripts": {
    "post-update-cmd": [
        "@php artisan erikaraujo-stub:publish --force"
    ]
}

*note that this has a force parameter, which will make the new stubs overwrite the existing ones on the stubs folder.

Usage

Publish the stubs using this artisan command:

php artisan erikaraujo-stub:publish

Options

  1. --force
php artisan erikaraujo-stub:publish --softdeletes

Unless you use --force, none of the existing stubs inside the ./stubs folder will be replaced.

  1. --softdeletes
php artisan erikaraujo-stub:publish --softdeletes

This will automatically add the SoftDeletes trait to your model stubs, add $table->softdeletes() to your migration stubs and add forceDelete() and restore() methods to the controllers.

  1. --inertia
php artisan erikaraujo-stub:publish --inertia

This will import Inertia\Inertia to all your non-api controller stubs by default as well as add resources to the controllers methods (if a model is provided).

Resource example:

public function index()
{
    return Inertia::render('{{ model }}/Index', [
        '{{ modelVariable }}' => {{ model }}::paginate()->onEachSide(1),
    ]);
}
  1. --json
php artisan erikaraujo-stub:publish --json

This will add resources to all your api controllers and return a json response with the correct HTTP response code.

Resource example:

public function index()
{
    ${{ modelVariable }} = {{ model }}::all();

    return response()->json([
        'data' => ${{ modelVariable }},
        'total' => ${{ modelVariable }}->count(),
    ], 200);
}
  1. Multiple You can mix and match these options and everything will be applied correctly. All examples below are fine:
php artisan erikaraujo-stub:publish --inertia --softdeletes
php artisan erikaraujo-stub:publish --json --softdeletes
php artisan erikaraujo-stub:publish --json --inertia --softdeletes
php artisan erikaraujo-stub:publish --json --inertia --softdeletes --force

Testing

TODO: Add testing to cover all possible mix and match scenarios.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

For now, just create a PR and I'll take a look.