acorn icon indicating copy to clipboard operation
acorn copied to clipboard

Illuminate/database support?

Open binaryfire opened this issue 4 years ago • 13 comments

Hi guys,

I'm looking into Wordpress MVC frameworks and am really liking the Bedrock/Sage/Acorn combo. I noticed illuminate/database was added in March 2019 but removed in Nov 2019. Just wondering what the reason for this was and if there are any issues using Eloquent with Acorn?

Also, does the Roots stack have any routing and middleware options? Or was that intentionally excluded in favour of doing it the Wordpress way?

Thanks :)

binaryfire avatar Oct 26 '21 12:10 binaryfire

I've been using Eloquent with Acorn for a while. Adding Illuminate\Database\DatabaseServiceProvider::class to config.app.providers and configuring 'config.database' was all that was needed. I have however not looked into how to handle migrations, opting instead to use dbDelta() for now.

stefanfisk avatar Oct 27 '21 06:10 stefanfisk

I've now also started using migrations, and so far it just works for me.

For the database support I required illuminate/database and then added the following config/database.php and App\Providers\DatabaseServiceProvider:

<?php

return [
    'default'     => 'wpdb',
    'connections' => [
        'wpdb' => [
            /// Defined in App\Providers\DatabaseServiceProvider::register()
        ],
    ],
    'migrations'  => 'migrations',
];
<?php

namespace App\Providers;

use Illuminate\Database\DatabaseServiceProvider as BaseDatabaseServiceProvider;
use wpdb;

class DatabaseServiceProvider extends BaseDatabaseServiceProvider
{
    public function register()
    {
        /** @var wpdb $wpdb */
        $wpdb = $GLOBALS['wpdb'];

        $connections = $this->app['config']['database.connections'];

        $connections['wpdb'] = [
            'driver'         => 'mysql',
            'host'           => DB_HOST,
            'database'       => DB_NAME,
            'username'       => DB_USER,
            'password'       => DB_PASSWORD,
            'charset'        => $wpdb->charset,
            'collation'      => $wpdb->collate,
            'prefix'         => $wpdb->prefix,
            'prefix_indexes' => true,
            'strict'         => true,
            'engine'         => null,
        ];

        $this->app['config']['database.connections'] = $connections;

        parent::register();
    }
}

And then for migrations support I just use Illuminate\Database\MigrationServiceProvider.

I think that you might want to add this to your composer.json to avoid loading the default DatabaseServiceProvider:

    "extra": {
        "laravel": {
            "dont-discover": [
                "*"
            ]
        }
    },

stefanfisk avatar Dec 12 '21 17:12 stefanfisk

@stefanfisk can you elaborate a bit more how you generate migration files and how you run them?

sammyaxe avatar May 31 '22 15:05 sammyaxe

@sammyaxe if you enable Illuminate\Database\MigrationServiceProvider the make:migration command should be available just like in Artisan.

stefanfisk avatar May 31 '22 15:05 stefanfisk

MigrationServiceProvider

Thanks for the answer, but it seems that some other service registers migrations, do you have any other services enabled?

sammyaxe avatar Sep 08 '22 02:09 sammyaxe

Which services specifically are registering migrations for you?

stefanfisk avatar Sep 08 '22 06:09 stefanfisk

So on Laravel setup, if I comment out Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class I am no longer able to see migration commands. I assume this is because of ArtisanServiceProvider::class that is called in ConsoleSupportServiceProvider, but the problem is that has so many more dependencies that I don't really need.

Did you made ConsoleSupportServiceProvider available in your case?

sammyaxe avatar Sep 09 '22 00:09 sammyaxe

I’ve never used ConsoleSupportServiceProvider, only MigrationServiceProvider and ComposerServiceProvider directly. ArtisanServiceProvider seems to only register commands, so I’m not sure why that would cause you any issues.

Could you provide specific information on what is no working, like error messages or something?

stefanfisk avatar Sep 09 '22 05:09 stefanfisk

The weird part is that I don't get any errors

These are the providers I'm registering, any chance you could share yours so I could compare, I'm sure I'm missing something minor.


 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Foundation\Providers\ComposerServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
        Roots\Acorn\Providers\AcornServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\ThemeServiceProvider::class,
        App\Providers\DatabaseServiceProvider::class

    ],

    ```
    

sammyaxe avatar Sep 09 '22 14:09 sammyaxe

@sammyaxe I don't see MigrationServiceProvider in the list.

stefanfisk avatar Sep 09 '22 14:09 stefanfisk

@stefanfisk I owe you a beer, man. Not sure how I did not notice that. Did you get seeders to work by any chance?

sammyaxe avatar Sep 09 '22 17:09 sammyaxe

@sammyaxe I've never tried using seeders with Acorn, but if you give it a try please let us know how it goes!

stefanfisk avatar Sep 13 '22 09:09 stefanfisk

So I think I got seeders to work. My solution is somewhat hacky, but make:seeder, db:wipe, db:seed commands appear to be working.

What I did was essentially created new Service provider and I called it CommandServiceProvider.php, when I copied all the code from ArtisanServiceProvider it has two arrays of commands it registers $commands and $devCommands

From here you can comment out/delete commands you do not need, for e.g. if you do not wish to install illuminate/auth you need to remove auth related commands, because that will cause problems otherwise, same with other commands like queues, notifications, routers etc.

I really don't mind having other laravel features available, so I installed

    "illuminate/auth": "8.70",
    "illuminate/notifications": "8.70",
    "illuminate/queue": "8.70",
    "illuminate/routing": "8.70",

however, I could not get router to work as it kept saying that router class was missing so I end up commenting that out from the array in my CommandServiceProvider.php

sammyaxe avatar Sep 20 '22 12:09 sammyaxe

Got a PR up for this at https://github.com/roots/acorn/pull/277

To get seeders working I had to copy https://github.com/laravel/laravel/blob/9.x/database/seeders/DatabaseSeeder.php (and load it)

retlehs avatar Feb 22 '23 05:02 retlehs