Publishing config and migrations from included package in Laravel
Asked Answered
M

9

6

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.

Mullein answered 30/3, 2016 at 17:58 Comment(0)
G
5

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/

Grebe answered 3/5, 2019 at 14:34 Comment(1)
running composer update solved the problem for me.Coenocyte
I
4

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.

Impala answered 20/2, 2022 at 11:7 Comment(0)
R
3

Try to publish the config separately and use the force switch

php artisan vendor:publish --tag=config --force
Rummy answered 30/3, 2016 at 20:17 Comment(0)
U
2

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.

Uncommunicative answered 30/3, 2016 at 23:29 Comment(1)
Thanks for your answer, the other package has its own service provider but I don't know how to call it.. Do you know how? ThanksMullein
P
1

Publish vendor migration :

php artisan vendor:publish --tag=migration

Publish vendor config :

php artisan vendor:publish --tag=config
Pavlodar answered 2/9, 2019 at 9:17 Comment(0)
M
0

I think you have publish the config files yourself

$this->publishes([
    'other-package/absolute/path/some-config.php' => config_path('my-package.php'),
]);
Micrometry answered 30/3, 2016 at 18:30 Comment(1)
Thanks for your answer, Seems weird that you would have to do this for all packages you include? But it could be!Mullein
M
0

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.

Mullein answered 31/3, 2016 at 7:46 Comment(0)
P
0

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

Pearlinepearlman answered 2/5, 2023 at 20:37 Comment(2)
Thanks for reminding me of the times when I still wrote PHP! :)Mullein
You disappointed me!! :)Pearlinepearlman
J
0

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
Justiciar answered 14/7 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.