Extending Laravel 5.5/5.6 Blueprint

laravel macro
laravel-builder::macro
laravel macroable

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 :(


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);
        });
    }
}

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​  Laravel Macros are a great way of extending Laravel’s core classes and add extra functionality required for our application. It is a way to add some missing functionality to Laravel’s internal


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
});

Extending The Framework - Laravel, To extend the Laravel cache facility, we will use the extend method on the CacheManager , which is used to bind a custom driver resolver to the manager, and is  Extending Laravel 5 Blueprint for MySqlConnection. GitHub Gist: instantly share code, notes, and snippets.


I faced the same problem and I solved it by making a new CustomBlueprint class. I wrote a separate blog on this. Just go there and you find your answers. Here I just give an example of migration file which have commonFields() method to add common fields in a model

    $schema = DB::connection()->getSchemaBuilder();

    $schema->blueprintResolver(function ($table, $callback) {
        return new CustomBlueprint($table, $callback);
    });

    $schema->create('roles', function (CustomBlueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->unique();
        $table->commonFields();
    });

I created CustomBlueprint class which have that method.

<?php
namespace App\common;
use Illuminate\Database\Schema\Blueprint;
class CustomBlueprint extends Blueprint
{
    public function commonFields()
    {
        $this->timestamp('created_at')->nullable();;
        $this->unsignedBigInteger('created_by')->nullable();
        $this->timestamp('updated_at')->nullable();;
        $this->unsignedBigInteger('updated_by')->nullable();
        $this->timestamp('deleted_at')->nullable();;
        $this->unsignedBigInteger('deleted_by')->nullable();
        $this->boolean('is_deleted')->default(0);
    }
}

link : custom-blueprint-in-laravel-mode

Upgrading, Support for PHP 5.5/5.6 has been dropped. Version 5.0.0 of the @return void */ public function up() { Schema::table('audits', function (Blueprint $table)  The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!


Just use this in migration:

public function up()
{
    $schema = \DB::getSchemaBuilder();

    $schema->blueprintResolver(function($table, $callback) {
        return new YourBlueprint($table, $callback);
    });

    $schema->create('table_name', function (YourBlueprint $table) {
    ...

Laravel 5.5/5.6 Changing Columns Not Working · Issue #22029 , Laravel Version: 5.4.x/5.5.x PHP Version: 7.1 Database Driver <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use extends Migration { /** * Run the migrations. tprj29 changed the title Laravel 5.4/5.5 Changing Columns Not Working Laravel 5.5/5.6  With the recently released 4th Version, Laravel became even better in numerous ways. Within this book, we will help you learn about both the old and new features of Laravel while developing various applications. Laravel Application Development Blueprints covers how to develop 10 different applications step-by-step using Laravel 4.


Create User Following system with Laravel 5.5 / 5.6, use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\​Schema; /** * Class CreateUsersTable */ class CreateUsersTable extends Migration The call to Cache::extend could be done in the boot method of the default App\Providers\AppServiceProvider that ships with fresh Laravel applications, or you may create your own service provider to house the extension - just don't forget to register the provider in the config/app.php provider array.


How to Create a Package in Laravel 5.6, Here's a look at creating our very own package in Laravel 5.6 from scratch using simple steps. use Illuminate\Database\Schema\Blueprint; use use Wisdmlabs\Todolist\Task; class TodolistController extends Controller  By extending the provided model classes (preferred and simplest method). By using the provided model trait (allows for extending another model class). Extending model. When creating a Eloquent model, instead of extending the standard Laravel model class, extend from the model class provided by this package:


shiftonelabs/laravel-nomad, x (5.5, 5.6, 5.7, 5.8, 6.x). This package uses auto package discovery. The service provider will automatically be registered. Laravel 5.0 - 5.4 (5.0, 5.1, 5.2  The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Before getting started, be sure to configure a database connection in config/database.php. To get started, create an Eloquent