Miguel Stevens
Miguel Stevens

Reputation: 9220

Publishing config and migrations from included package in Laravel

I'm using Laravel to build a package which will include some Admin functionality and will include some other packages.

I have included a package which has migrations and a config file, But how do I copy those to the correct folder in Laravel. The package has it's own ServiceProvider but How do I call that from my package?

I'm using the following to register the package in my package.

class CldServiceProvider extends ServiceProvider {

    public function register()
    {
       $this->app->register(MediaLibraryServiceProvider::class);
    }
}

I've tried

php artisan vendor:publish 

But it says there's nothing to publish. Because it's only looking at the packages for this Laravel installation, and not the nested packages.

Upvotes: 7

Views: 25462

Answers (9)

John Lobo
John Lobo

Reputation: 15319

For laravel 11 ,Configuration Publishing

Most of Laravel's configuration files are already published in your application's config directory; however, certain configuration files like cors.php and view.php are not published by default, as most applications will never need to modify them.

However, you may use the config:publish Artisan command to publish any configuration files that are not published by default:

php artisan config:publish
 
php artisan config:publish --all

Upvotes: 0

majid mohammadian
majid mohammadian

Reputation: 83

you have running this code, this code must be written in boot method.

// run dependency publishable
$this->publishes(self::pathsToPublish(MediaLibraryServiceProvider::class), 'sample-group');

You can go to the example written in the boot function in this package

Upvotes: 0

relliv
relliv

Reputation: 863

Create database/migrations folder in your package root folder. Then add publish details in boot method in your service provider.

final class MyPackageServiceProvider extends BaseServiceProvider
{
    public function boot(): void
    {
        $this->publishes([
            __DIR__ . '/../database/migrations/' => database_path('migrations/my-package'),
        ], 'my-package-migrations');
    }
}

Second part could be like database_path('migrations') or database_path('migrations/my-package'). I advise using with subfolder because if there are many packages migrations folder grows too much.

The part of 'my-package-migrations' is our tag. And run publish command with tag.

php artisan vendor:publish --tag=my-package-migrations

If you are using the subfolder php artisan migrate command won't work because we need to specify to exact migration folder like this,

php artisan migrate --path=/database/migrations/my-package

Yes, this looks a little bit dirty but organizes cables. And don't forget the using composer dump-autoload. If the service provider is registered this solution works. Also, you can use this method for config but config folder does not support subfolders.

Upvotes: 5

Vahid
Vahid

Reputation: 11

Publish vendor migration :

php artisan vendor:publish --tag=migration

Publish vendor config :

php artisan vendor:publish --tag=config

Upvotes: 1

lewis
lewis

Reputation: 3202

In my case I found I needed to include MY package in the application I was working on. This step was not included in the original tutorial I followed.

So in composer.json in the main app:

"repositories": [
    {
        "type": "path",
        "url": "packages/namespace/name",
        "options": {
            "symlink": true
        }
    }
],
"require": {
    // ...
    "namespace/name": "dev-master"
},

And then we run this command from main folder:

composer update

After that running php artisan vendor:publish will include the option to publish the sub packages.

NB: The source for this info is: https://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/

Upvotes: 5

Miguel Stevens
Miguel Stevens

Reputation: 9220

Solution

I included the /packages/ folder in my current project where I was working on. The problem is that when including the package in my composer.json

It is now reachable from 2 places inside my project, in the vendor folder and in my packages folder. This was causing issue's

I moved my packages outside my current project, this fixed the problem.

Upvotes: 0

Andrew Miller
Andrew Miller

Reputation: 23

As @scrubmx mentioned, you need to include the code that defines what can be published, though this code shouldn't really be in your own service provider, but rather the other package you're including. If it doesn't seem to have this code, it's best to list it as an issue on that package's repository or create a pull request to add it.

Upvotes: 2

Michael Smith
Michael Smith

Reputation: 665

Try to publish the config separately and use the force switch

php artisan vendor:publish --tag=config --force

Upvotes: 3

scrubmx
scrubmx

Reputation: 2556

I think you have publish the config files yourself

$this->publishes([
    'other-package/absolute/path/some-config.php' => config_path('my-package.php'),
]);

Upvotes: 0

Related Questions