Lädt...

🔧 Simplify Laravel Migrations with Macros


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Have you ever found yourself repeatedly writing the same fields in your Laravel migrations?

It can be tedious, right? But guess what, there’s a much easier way to handle this! Introducing Laravel Blueprint Macros.

Blueprint Macros allow you to extend Laravel’s migration schema functionality and add custom logic as needed. This way, you can avoid repeating the same fields in your migrations and increase your productivity.

Let me show you how.

Before Using Blueprint Macros

You might have this repetitive code in your migrations :

Schema::create('candidates', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->string('phone_number');
    $table->string('email');
    $table->string('fax');
    $table->timestamps();
});

Schema::create('employees', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->string('rank');
    $table->string('occupancy');
    $table->timestamps();
});

As you can see, the fiels name, email and password is repeated in that two tables. Now imagine doing this for every migration file in your project. Tedious, right?

After Using Blueprint Macros

Now, let’s use Blueprint Macros to simplify things!

  1. First, define a macro in your AppServiceProvider.php or any service provider:
use Illuminate\Database\Schema\Blueprint;

....

public function boot()
{
    Blueprint::macro('accountFields', function () {
        $this->string('name');
        $this->string('email')->unique();
        $this->string('password');
    });
}
  1. Then, apply it in your migrations like this :
Schema::create('candidates', function (Blueprint $table) {
    $table->id();
    $table->accountFields();
    $table->string('phone_number');
    $table->string('email');
    $table->string('fax');
    $table->timestamps();
});

Schema::create('employees', function (Blueprint $table) {
    $table->id();
    $table->accountFields();
    $table->string('rank');
    $table->string('occupancy');
    $table->timestamps();
});

By defining the accountFields macro, you avoid repeating the same column definitions across multiple migration files. It’s clean, efficient, and easy to manage.

Why Use Blueprint Macros? :

  • Reusable: Write once, use everywhere.
  • DRY Principle: Avoid repeating yourself.
  • Clean Code: Keep your migration files neat and easy to read.

If you found this tip helpful, don’t forget to share it with others who might benefit from it!

...

🔧 Simplify Laravel Migrations with Macros


📈 52.2 Punkte
🔧 Programmierung

🔧 Avoid data migrations in the schema migrations for Rails


📈 32.33 Punkte
🔧 Programmierung

🔧 Simplify Database Migrations using Python with Alembic


📈 27.77 Punkte
🔧 Programmierung

🎥 Upgrade Laravel 11 to Laravel 12 #laravel #thecodeholic


📈 26.52 Punkte
🎥 Video | Youtube

🎥 Using Laravel Breeze Starter Kits in Laravel 12 #laravel #thecodeholic #laravel12


📈 26.52 Punkte
🎥 Video | Youtube

🎥 Print SQL Queries in Laravel #laravel #thecodeholic #laravelcourse #php #laravel


📈 26.52 Punkte
🎥 Video | Youtube

🎥 Create First Laravel Project using Laravel Herd #laravel #laravelproject #laravelphp


📈 26.52 Punkte
🎥 Video | Youtube

🔧 [Laravel v11 x Docker] Efficiently Set Up a Laravel App Dev Environment with Laravel Sail


📈 26.52 Punkte
🔧 Programmierung

🔧 Supercharge Laravel Workflow with Git Hooks: Automate Migrations and Caching Only When You Need It


📈 25.01 Punkte
🔧 Programmierung

🔧 Simplifying SQL View Management in Laravel Migrations


📈 25.01 Punkte
🔧 Programmierung

🔧 Migrations, Quiry Builder, dan Eloquent Dalam Laravel


📈 25.01 Punkte
🔧 Programmierung

🔧 Migrations in Laravel


📈 25.01 Punkte
🔧 Programmierung

🔧 Database Schema Management in Laravel Using Migrations: An In-Depth Tutorial


📈 25.01 Punkte
🔧 Programmierung

🔧 Handling Timestamp Column Changes in Laravel Migrations with PostgreSQL


📈 25.01 Punkte
🔧 Programmierung

🔧 Run custom migrations in laravel


📈 25.01 Punkte
🔧 Programmierung

🔧 Introducing a New Laravel Package DumpTable for Seamless Migrations


📈 25.01 Punkte
🔧 Programmierung

🎥 Generate Models and Migrations - Part 2 | Laravel Social Media Website


📈 25.01 Punkte
🎥 Video | Youtube

🔧 The Art of Database Migrations in Laravel -- Common Mistakes


📈 25.01 Punkte
🔧 Programmierung

🔧 Database Migrations in Laravel


📈 25.01 Punkte
🔧 Programmierung

🔧 How to Create, Run, & Manage Laravel Migrations


📈 25.01 Punkte
🔧 Programmierung

🔧 Laravel Migrations: A Beginner-Friendly Guide 🚀


📈 25.01 Punkte
🔧 Programmierung

🔧 Laravel SQLSTATE[42000] error during migrations and its solutions


📈 25.01 Punkte
🔧 Programmierung

🔧 Resolving the SQLSTATE[42000] Error in Laravel Migrations: Key Length Issue


📈 25.01 Punkte
🔧 Programmierung

🔧 Laravel migrations


📈 25.01 Punkte
🔧 Programmierung

🔧 Laravel Under The Hood - A Little Bit of Macros


📈 24.43 Punkte
🔧 Programmierung

🔧 Macros & Mixins in Laravel and additional development features


📈 24.43 Punkte
🔧 Programmierung

🔧 Simplify Eloquent Query Filtering in Laravel with Eloquent Filter


📈 20.44 Punkte
🔧 Programmierung

🔧 🎉 Simplify Laravel CRUD Operations with Ease! 🚀


📈 20.44 Punkte
🔧 Programmierung

🔧 Introducing Laravel API Version: Simplify API Versioning


📈 20.44 Punkte
🔧 Programmierung

🔧 Laravel Relationship Recipes: Simplify Querying with hasManyThrough


📈 20.44 Punkte
🔧 Programmierung

🔧 Laravel Relationship Recipes: Simplify Querying with newestOfMany


📈 20.44 Punkte
🔧 Programmierung

🔧 Laravel Relationship Recipes: Simplify Querying with oldestOfMany


📈 20.44 Punkte
🔧 Programmierung

🔧 Introducing Filterable: Simplify Data Handling in Your Laravel Applications


📈 20.44 Punkte
🔧 Programmierung

matomo