Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrcorex's avatar

illuminate/database (+ migrations) outside Laravel

Hey guys. I am trying to find a good solution to use illuminate/database (+ migrations) outside Laravel.

I have the need of several packages (non-public) that are targeted for specific purposes. And they are going to be used in several projects, both Laravel and non-Laravel.

illuminate/database already requires illuminate/container, so I will of course go this way.

My goal is to have

  • Query builder
  • Models
  • Migrations

I am not new to Laravel, but need some guidance/ideas/comments in writing these packages. I have not been able to find anything on Google. There are only the classic titles "laravel migrations outside Laravel" and when you go to the page, it is all about Phinx.

It is not about the first 2 (Query Builder and Models) since https://packagist.org/packages/illuminate/database explains it very well. I will however try to implement facades outside Laravel so developers still can use Laravels excellent documentation. I can implement facades by extending a facade class and override getFacadeApplication().

I have 3 questions:

  1. Do any of you know of any existing package(s) that include migration support?

  2. If no known packages, I can write a public package if there are the need for it. Any comments on that?

  3. How would you experienced Laravel Developers implement migrations?

0 likes
9 replies
jlrdw's avatar

Are you saying you already know how to set up the database taken from here. https://github.com/illuminate/database/tree/6.x

Which I've used standalone also. I didn't worry about migrations, rather I just used a front. In my case I use sqlyog, but there are others.

Seems migrations are in that package.

Curious, if you need that much, why not just laravel all the way.

mrcorex's avatar

I do know how to setup database and all that. Using Laravel already. And I prefer to use Laravel all the way, but most of the projects are not Laravel and I want to make one unified layer of packages that can be distributed in various projects. I hate redundant code :)

I have been thinking of using a small Laravel/Lumen installation only for migrations or some other tool. But if I can have it all in one base package, it would be nice.

DPlachkov's avatar

Hello. I've made it so that you use migrations and illuminate/database as standalone. Yes you do use phinx. You do not need the service container to achieve this.All you need is illuminate/database and phinx

This is my phinx.php file located in my root directory. The bootstrap.php is a file that holds my global constants and configs. The important thing here is the 'migration_base_class' => '\Database\Migration',

<?php
/**
 * Created by Deyan Plachkov.
 * Email: [email protected]
 * Date: 6/25/2019
 * Time: 3:27 PM
 */
# Load composer
require 'vendor/autoload.php';

require_once(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.php');

return [
    'paths' => [
        'migrations'    => '%%PHINX_CONFIG_DIR%%/database/migrations',
        'seeds'         => '%%PHINX_CONFIG_DIR%%/database/seeds',
    ],
    'templates' => [
        'file' => 'phinx-template.php.dist'
    ],
    'migration_base_class' => '\Database\Migration',
    'environments' => [
        'default_migration_table' => 'phinxlog',
        'default_database' => 'default',
        'default' => [
            'adapter' => 'mysql',
            'host' => DATABASE_HOST,
            'name' => DATABASE_DATABASE,
            'user' => DATABASE_USERNAME,
            'pass' => DATABASE_PASSWORD,
            'port' => 3306
        ]
    ]
];

Next up is the migration class itself - keep in mind that i've placed it in a namespace Database which you have to add to composer ( "Database\": "database/", ) Keep in mind that the 2 rows

          $platform = $this->capsule->getConnection()->getDoctrineSchemaManager()->getDatabasePlatform();
           $platform->registerDoctrineTypeMapping('enum', 'string');

Are here trying to deal with the ENUM problems. You can read more about the enum problems in the official documentation.

<?php namespace Database;

require_once(__DIR__ . '/../config/bootstrap.php');

use Illuminate\Database\Capsule\Manager as Capsule;
use Phinx\Migration\AbstractMigration;
use Doctrine\DBAL\Types\{StringType, Type};

class Migration extends AbstractMigration {
    /** @var \Illuminate\Database\Capsule\Manager $capsule */
    public $capsule;
    /** @var \Illuminate\Database\Schema\Builder $capsule */
    public $schema;

    public static $CAPSULE;

    public function init()  {

        if (self::$CAPSULE) {
            $this->capsule = self::$CAPSULE;
            $this->schema = $this->capsule->schema();
        } else {
            $this->capsule = new Capsule;
            $this->capsule->addConnection([
                'driver'    => DATABASE_DRIVER,
                'host'      => DATABASE_HOST,
                'database'  => DATABASE_DATABASE,
                'username'  => DATABASE_USERNAME,
                'password'  => DATABASE_PASSWORD,
                'charset'   => DATABASE_CHARSET,
                'collation' => DATABASE_COLLATION,
                'prefix'    => DATABASE_PREFIX,
                'engine'    => 'InnoDB'
            ]);

            $platform = $this->capsule->getConnection()->getDoctrineSchemaManager()->getDatabasePlatform();
            $platform->registerDoctrineTypeMapping('enum', 'string');

            $this->capsule->bootEloquent();
            $this->capsule->setAsGlobal();
            $this->schema = $this->capsule->schema();

            self::$CAPSULE = $this->capsule;
        }
    }
}

Here is an example migration I have

<?php

use \Database\Migration;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;

class EditBagsAddLinkedBag extends Migration
{
    public function up()
    {
        Capsule::schema()->table('bags', function (Blueprint $table) {
            $table->integer('actual')->nullable()->after('id');
        });
    }

    public function down()
    {
        Capsule::schema()->table('bags', function (Blueprint $table) {
           $table->dropColumn('actual');
        });
    }
}

Here are some scripts in the composer.json which you can use

"scripts": {
        "create": "phinx create",
        "migrate": "phinx migrate",
        "migrations": "phinx status",
        "rollback": "phinx rollback",
}

Keep in mind that the difference here is that those migrations are saved in "phinxlog" instead of the "migrations" table. Maybe this behaviour can be altered, but its fine by me

mrcorex's avatar

Hi DPlachkov and thanks for your answer.

I can see I made an incomplete question. My bad :(

The question should have been: Do any of you know of any existing package(s) that include Laravel's migration support?

I know about the use of Phinx, but I want to use Illuminate all over :)

The packages I need to write must work both inside and outside Laravel, so it will make sence to use Illuminate all over.

I'm sorry for the confusion this has caused :|

arul's avatar

Check out https://github.com/Luracast/Laravel-Database it wraps eloquent capsule to lazy load when used in the web application and provides all artisan DB-related commands and more on the console.

It is recently upgraded to support laravel 8 components

Available commands:

  dump-autoload     Regenerate framework autoload files
  env               Display the current framework environment
  help              Display help for a command
  inspire           Display an inspiring quote
  list              List commands
  migrate           Run the database migrations
  serve             Serve the application on the PHP development server
  tinker            Interact with your application
  
  db
  db:seed           Seed the database with records
  
  make
  make:command      Create a new Artisan command
  make:controller   Create a new controller class
  make:factory      Create a new model factory
  make:migration    Create a new migration file
  make:model        Create a new Eloquent model class
  make:seeder       Create a new seeder class
  
  migrate
  migrate:fresh     Drop all tables and re-run all migrations
  migrate:install   Create the migration repository
  migrate:refresh   Reset and re-run all migrations
  migrate:reset     Rollback all database migrations
  migrate:rollback  Rollback the last database migration
  migrate:status    Show the status of each migration
  
  vendor
  vendor:publish    Publish any publishable assets from vendor packages

By the way, I'm the author of that!

1 like
RoniWasHere's avatar

@arul How would you run this command outside of Laravel?

I did composer require illuminate/database - it worked,

but how can I run php artisan commands outside of Laravel?

Please or to participate in this conversation.