Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

What is stubs in laravel

What are stubs in laravel?

Today, I will give you an example of “What is Stub, how to use it and customize Stubs in Laravel”, So you can easily apply it with your laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

generate stubs in laravel
generate soft delete automatically using stub

What are Stubs in Laravel

Laravel 7 and 8 came with a new feature, stub customization, All these stubs contain the basic content that is used to generate the files that are created with the artisan make command.

After publishing these stubs, you have the ability to edit their content.

Customize Stubs in Laravel

We can customize any stubs, in this example, we will customize migration and model stubs.

Sometimes, we requirements for soft delete in many tables in the laravel application, so we add the soft delete trait in every migration file and every model. So using Stubs and customizing stubs we simply write soft delete functionality in stub, it will save our time, and automatically it will generate soft delete trait in every model and migration.

Let’s see what happens when we execute the command to enable customization:

php artisan stub:publish

You will see the “Stubs published successfully” message, and a new folder stub becomes visible in the root of your project, containing files that are customizable.

stubs generated using command

Customize Migration and Model Stub to Generate Softdeletes trait automatically.

stubs\migration.create.stub

<?php

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

class {{ class }} extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('{{ table }}', function (Blueprint $table) {
            $table->id();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('{{ table }}');
    }
}

stubs\model.stub

<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class {{ class }} extends Model
{
    use SoftDeletes;
    use HasFactory;
}

Let’s create a Migration and Model to check Softdeletes trait is added or not automatically.

Generate Migration

php artisan make:migration create_test_stubs_table

Run Migration

php artisan migrate

database\migrations\CreateTestStubsTable

<?php

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

class CreateTestStubsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('test_stubs', function (Blueprint $table) {
            $table->id();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('test_stubs');
    }
}

Create Model

php artisan make:model TestStub

app\Models\TestStub.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TestStub extends Model
{
    use SoftDeletes;
    use HasFactory;
}

Output:-

output after customizing stub in laravel

We successfully customize “Stubs in Laravel”, I hope this article will help you with your Laravel application Project.

Read also: Restore back deleted record in Laravel.