3 ways to extend Laravel classes
Laravel is a PHP framework which offers a rich set of functionalities out of the box. Most of the times the framework provides everything you need to fulfill your project requirements. But sometimes you may find yourself in the situation that you want to extend a existing Laravel class with own functionality to fit to your requirements.
A few weeks ago I had the requirement to add the same database columns to a lot of tables. The easiest solution would be to add the same statement multiple times in different migration files. This solution would mean duplicated code which I wanted to avoid. I asked myself: What if I could extend the Laravel Blueprint Class?
In this article I want to you to show three ways how you can do exactly this. You will learn how you can achieve your goal with PHP inhericante, Laravel macros and PHP traits.
1. Laravel macros
Laravel macros are a great way to extend existing classes with new methods. You can add new methods to existing classes without touching the original class. This is a great way to keep your code clean and maintainable.
In my case I wanted to add a new method to the Laravel Blueprint class. The Blueprint class is used to define the structure of a database table. I wanted to add a new method to the Blueprint class which adds the same columns to a table.
To achieve this I created a new service provider and added the following code:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\ServiceProvider;
class BlueprintServiceProvider extends ServiceProvider
{
public function boot()
{
Blueprint::macro('addSameColumns', function () {
$this->string('name');
$this->string('email');
$this->timestamps();
});
}
}
After that I registered the service provider in the config/app.php
file:
'providers' => [
// Other Service Providers
App\Providers\BlueprintServiceProvider::class,
],
Now I can use the new method in my migration files:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->addSameColumns();
});
2. PHP inheritance
Another way to extend Laravel classes is to use PHP inheritance. You can create a new class which extends the original class and add your own functionality to it.
In my case I wanted to add a new method to the Laravel Blueprint class. The Blueprint class is used to define the structure of a database table. I wanted to add a new method to the Blueprint class which adds the same columns to a table.
To achieve this I created a new class which extends the Laravel Blueprint class:
use Illuminate\Database\Schema\Blueprint;
class MyBlueprint extends Blueprint
{
public function addSameColumns()
{
$this->string('name');
$this->string('email');
$this->timestamps();
}
}
Now I can use the new class in my migration files:
use App\MyBlueprint;
Schema::create('users', function (MyBlueprint $table) {
$table->id();
$table->addSameColumns();
});
3. PHP traits
PHP traits are a great way to add functionality to classes. You can create a trait which contains the functionality you want to add and then use the trait in your class.
In my case I wanted to add a new method to the Laravel Blueprint class. The Blueprint class is used to define the structure of a database table. I wanted to add a new method to the Blueprint class which adds the same columns to a table.
To achieve this I created a new trait which contains the new method:
use Illuminate\Database\Schema\Blueprint;
trait MyBlueprintTrait
{
public function addSameColumns()
{
$this->string('name');
$this->string('email');
$this->timestamps();
}
}
Now I can use the trait in my migration files:
use App\MyBlueprintTrait;
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->addSameColumns();
});
Conclusion
In this article you learned three ways how you can extend Laravel classes to make your code more maintainable and flexible.
Laravel macros are a great way to add new methods to existing classes without touching the original class. PHP inheritance allows you to create a new class which extends the original class and add your own functionality to it. PHP traits are a great way to add functionality to classes by creating a trait which contains the functionality you want to add and then using the trait in your class.
Choose the method that best fits your needs based on the specific use case and the level of customization you require. The best approach may depend on the complexity and nature of the functionality you want to add or modify.
Yours sincerely, Florian.