roots / acorn

Laravel components for WordPress plugins and themes
https://roots.io/acorn/
MIT License
818 stars 94 forks source link

Illuminate/database support? #119

Closed binaryfire closed 1 year ago

binaryfire commented 3 years ago

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

stefanfisk commented 3 years ago

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 commented 2 years ago

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": [
                "*"
            ]
        }
    },
sammyaxe commented 2 years ago

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

stefanfisk commented 2 years ago

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

sammyaxe commented 2 years ago

MigrationServiceProvider

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

stefanfisk commented 2 years ago

Which services specifically are registering migrations for you?

sammyaxe commented 2 years ago

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?

stefanfisk commented 2 years ago

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?

sammyaxe commented 2 years ago

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

    ],
stefanfisk commented 2 years ago

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

sammyaxe commented 2 years ago

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

stefanfisk commented 2 years ago

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

sammyaxe commented 2 years ago

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

retlehs commented 1 year ago

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)