Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Laravel 5.5/5.6 Blueprint

How to extend Laravel 5.5/5.6 MySQL Blueprint with custom method $table->myMethod() like pre-built method $table->timestamps();?

I have tried an example with SQLlite: Extending Blueprint for SQLite in Laravel 5

But I couldn't get it work. I am developing my own package (using package development best practices) and would like include the Blueprint extension from there.

My devloped package has Laravel extension folder "Extension": ./packages/vendorname/packagename/src/Extension/

There are files:

1) ./packages/vendorname/packagename/src/Extension/Blueprint.php

<?php

namespace Vendorname\Packagename\Extension;

use Illuminate\Database\Schema\Blueprint as ParentBlueprint;
use Illuminate\Support\Facades\DB;

class Blueprint extends ParentBlueprint
{

     public function myMethod()
     {
          $this->text('custom_column')->default('Some value');
          $this->text('custom_column2')->default('Some value2');
     }
}

2) ./packages/vendorname/packagename/src/Extension/MySqlConnection.php

<?php

namespace Vendorname\Packagename\Extension;

use Vendorname\Packagename\Extension\Blueprint;
use Illuminate\Database\MySqlConnection as ParentMySqlConnection;
use Illuminate\Database\Schema\MySqlBuilder;

class MySqlConnection extends ParentMySqlConnection
{

     public function getSchemaBuilder()
     {
          if (is_null($this->schemaGrammar)) {
               $this->useDefaultSchemaGrammar();
          }

          $builder = new MySqlBuilder($this);
          $builder->blueprintResolver(function ($table, $callback) {
               return new Blueprint($table, $callback);
          });

          return $builder;
     }
}

And my package has service provider: ./packages/vendorname/packagename/src/VendornamePackagenameServiceProvider.php with register() method:

public function register()
{      if ($this->app->config->get('vendorname-packagename') === null) {
          $this->app->config->set('vendorname-packagename', require __DIR__.'/../config/vendorname-packagename');
     }

     $this->app->bind('db.connection.mysql', Vendorname\Packagename\Extension\MySqlConnection::class);
}

Also my package has migration file: ./packages/vanilla/vanilla-admin/src/migrations/2018_02_07_213437_create_logs_table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
//use Vendorname\Packagename\Extension\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLogsTable extends Migration
{

    public function up()
    {
        Schema::create('logs', function (Blueprint $table) {
            $table->increments('id');
            $table->text('package');
            $table->longText('log');
            $table->myMethod();
            $table->timestamps();
        });
    }

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

How can I use standard "use Illuminate\Database\Schema\Blueprint;" in migration, beacuse I don't want to use "use Vendorname\Packagename\Extension\Blueprint;".

Also I got an error on running migration "php artisan migrate:refresh":

Type error: Argument 1 passed to CreateLogsTable::{closure}() must be a
  n instance of Vanilla\VanillaAdmin\Extension\Blueprint, instance of Ill
  uminate\Database\Schema\Blueprint given, called in /Users/raido/.compos
  er/vendor/bin/laravel-packages/vendor/laravel/framework/src/Illuminate/
  Database/Schema/Builder.php on line 164

Please help me find a solution. I searched over the internet and tested whole day, I couldn't get it work :(

like image 276
Raido Orumets Avatar asked Mar 05 '18 15:03

Raido Orumets


2 Answers

I found this when searching for the same question. I'm using Laravel 5.8 and do it this way.

  1. Create custom Blueprint class
namespace App\Database\Schema;

class Blueprint extends \Illuminate\Database\Schema\Blueprint
{
    public function datetimeCreatedAt($precision = 0)
    {
        return $this->dateTime('created_at', $precision);
    }

}
  1. Create MySqlConnection class or what ever you need
namespace App\Database;

class MySqlConnection extends \Illuminate\Database\MySqlConnection
{
    public function getSchemaBuilder()
    {
        $builder = parent::getSchemaBuilder();
        $builder->blueprintResolver(function ($table, $callback) {
            return new \App\Database\Schema\Blueprint($table, $callback);
        });

        return $builder;
    }
}
  1. Change AppServiceProvider
namespace App\Providers;

use App\Database\MySqlConnection;
use Illuminate\Database\Connection;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
            return new MySqlConnection($connection, $database, $prefix, $config);
        });
    }
}
like image 130
SiZE Avatar answered Oct 03 '22 15:10

SiZE


With Laravel 5.5, the best way to extend Blueprint is to register macros in your AppServiceProvider boot method like so:

Blueprint::macro('yourmethod', function () {  
    // your method here
});
like image 30
Paras Avatar answered Oct 03 '22 15:10

Paras