Laravel Stubs: Enhance Your Code Templates

techiydude
5 min readNov 24, 2024

--

Laravel stubs are an incredible feature that can significantly enhance your development workflow. Whether you’re generating controllers, models, or other boilerplate files, stubs help automate repetitive tasks. But what if your stubs could do more? What if they could dynamically adapt to your needs, creating smarter, more customized code for your project?

In this article, we will take a deep dive into Laravel stubs. We will cover:

  • What stubs are and why they are useful
  • How to customize Laravel stubs
  • Examples for every type of stub
  • Adding dynamic behavior to stubs (Next Article)
  • Practical examples for real-world applications (Next Article)
  • Interview questions to test your understanding

By the end, you’ll not only master the basics of Laravel stubs but also learn how to unlock their full potential using dynamic logic.

Catchy Introduction

Have you ever been frustrated with repetitive boilerplate code when creating resources like controllers or migrations in Laravel? Stubs are here to save the day. They allow you to define the structure of generated files, ensuring consistency and speeding up development.

But what if you could take it further, adding dynamic behavior, automating relationships, or injecting custom logic into your generated code? Let’s dive into the world of Laravel stubs and unlock their full potential.

What Are Laravel Stubs?

In Laravel, stubs are plain text files that serve as templates for generating code. When you use Artisan commands like make:model or make:controller, Laravel relies on these stubs to determine the structure of the generated files.

php artisan make:controller UserController

Laravel uses the controller.stub file to create the boilerplate code for the UserController.

Why Are Stubs Useful?

  1. Time-Saving: Automate repetitive coding tasks.
  2. Consistency: Ensure all generated files follow the same structure.
  3. Customizable: Tailor stubs to meet your project’s requirements.

Customizing Stubs in Laravel

Sometimes, the default stubs might not meet your project’s specific requirements. Laravel allows you to customize these stubs to fit your needs. Here’s how:

  1. Publish Stubs:
    Use the following command to publish stubs into your project:
php artisan stub:publish

This copies the default stubs into the /stubs directory of your Laravel project.

2. Edit Stubs:
Modify the content of the stubs to include custom boilerplate code, such as namespace adjustments or specific method stubs.

3. Use Customized Stubs:
Once edited, these stubs will be used by Artisan commands automatically.

Laravel Default Stub Files.

When you publish Laravel stubs, we will see the following files in resources/stubs:

In Laravel’s latest version (Laravel 11) they provide some more stubs.

├── cast.inbound.stub
├── cast.stub
├── class.invokable.stub
├── class.stub
├── console.stub
├── controller.api.stub
├── controller.invokable.stub
├── controller.model.api.stub
├── controller.model.stub
├── controller.nested.api.stub
├── controller.nested.singleton.api.stub
├── controller.nested.singleton.stub
├── controller.nested.stub
├── controller.plain.stub
├── controller.singleton.api.stub
├── controller.singleton.stub
├── controller.stub
├── enum.backed.stub
├── enum.stub
├── event.stub
├── factory.stub
├── job.queued.stub
├── job.stub
├── listener.queued.stub
├── listener.stub
├── listener.typed.queued.stub
├── listener.typed.stub
├── mail.stub
├── markdown-mail.stub
├── markdown-notification.stub
├── middleware.stub
├── migration.create.stub
├── migration.stub
├── migration.update.stub
├── model.pivot.stub
├── model.stub
├── notification.stub
├── observer.plain.stub
├── observer.stub
├── pest.stub
├── pest.unit.stub
├── policy.plain.stub
├── policy.stub
├── provider.stub
├── request.stub
├── resource-collection.stub
├── resource.stub
├── rule.stub
├── scope.stub
├── seeder.stub
├── test.stub
├── test.unit.stub
├── trait.stub
└── view-component.stub

I will provide examples of stubs from the previous version in this article. If you’re looking for examples of stubs from the latest version, feel free to leave a message in the comments section.

├── controller.api.stub
├── controller.model.stub
├── controller.plain.stub
├── controller.stub
├── factory.stub
├── migration.create.stub
├── migration.stub
├── model.stub
└── seeder.stub

Here’s a breakdown of each stub and how to use it:

1. controller.api.stub

Used for generating API controllers with resource methods like index, store, update, and destroy.

Default Stub Content:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class {{ class }} extends Controller
{
public function index()
{
//
}

public function store(Request $request)
{
//
}

public function update(Request $request, $id)
{
//
}

public function destroy($id)
{
//
}
}

Example Command:

php artisan make:controller Api/ProductController --api

2. controller.model.stub

Used for generating controllers tied to specific models.

Default Stub Content:

<?php

namespace App\Http\Controllers;

use App\Models\{{ model }};
use Illuminate\Http\Request;

class {{ class }} extends Controller
{
//
}

Example Command:

php artisan make:controller ProductController --model=Product

3. controller.plain.stub

A basic controller with no additional methods or logic.

Default Stub Content:

<?php

namespace App\Http\Controllers;

class {{ class }} extends Controller
{
//
}

Example Command:

php artisan make:controller BasicController --plain

4. controller.stub

The default stub for creating standard controllers. It includes placeholders for custom methods.

Default Stub Content:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class {{ class }} extends Controller
{
public function index()
{
//
}
}

Example Command:

php artisan make:controller HomeController

5. factory.stub

Defines the structure for model factories used for seeding data.

Default Stub Content:

<?php

namespace Database\Factories;

use App\Models\{{ model }};
use Illuminate\Database\Eloquent\Factories\Factory;

class {{ class }} extends Factory
{
protected $model = {{ model }}::class;

public function definition()
{
return [
//
];
}
}

Example Command:

php artisan make:factory ProductFactory

6. migration.create.stub

Used for generating migrations with a predefined up method for creating tables.

Default Stub Content:

<?php

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

return new class extends Migration
{
public function up()
{
Schema::create('{{ table }}', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('{{ table }}');
}
};

Example Command:

php artisan make:migration create_products_table

7. migration.stub

The generic migration stub for creating or modifying database tables.

Default Stub Content:
Similar to migration.create.stub, but without predefined table logic.

8. model.stub

Defines the structure of a basic model.

Default Stub Content:

<?php

namespace App\Models;

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

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

Example Command:

php artisan make:model Product

9. seeder.stub

Used for creating seeders to populate database tables.

Default Stub Content:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class {{ class }} extends Seeder
{
public function run()
{
//
}
}

Example Command:

php artisan make:seeder ProductSeeder

These are all examples of stubs. You can modify the content of these stubs to include custom boilerplate code, such as adjusting namespaces or adding specific method stubs.

Example of Modified Stub (controller.plain.stub)

Like this, we can modify any of the stubs. Once edited, these customized stubs will automatically be used by Artisan commands.

Adding Dynamic Behavior to Stubs

Stubs can be enhanced with dynamic placeholders and logic. For example, you can use a custom Artisan command to generate controllers with optional resource methods or add default traits. In the next article, I will explain dynamic behavior and custom stubs.

Interview Questions

  1. What are Laravel stubs, and why are they used?
  2. How do you publish Laravel’s default stubs?
  3. Explain the difference between controller.api.stub and controller.model.stub.
  4. How would you customize a factory stub to include default attributes?
  5. How can you add dynamic behavior to a stub using a custom Artisan command?
  6. What is the purpose of the migration.create.stub file?

Conclusion

Laravel stubs are a game-changer for any developer looking to save time and ensure consistency in their codebase. By customizing stubs and adding dynamic behavior, you can tailor Laravel’s code generation to meet even the most complex project requirements.

With the steps outlined in this guide, you are now equipped to master Laravel stubs and make your development process smarter and faster. Go ahead, experiment, and take your Laravel skills to the next level!

You can also create custom stubs. In the next article, I will explain how to create custom stubs and explore the dynamic behavior of stubs. Stay tuned!

--

--

techiydude
techiydude

Written by techiydude

I’m a developer who shares advanced Laravel and Node.js insights on Medium.

Responses (10)