简体   繁体   English

如何从 Laravel 包加载播种机而不发布它们?

[英]How to load seeders from Laravel packages, without publishing them?

I'm developing some packages for Laravel (for internal use).我正在为 Laravel 开发一些包(供内部使用)。

It is logical for a package, to have its own seeders.一个包有自己的播种机是合乎逻辑的。

For example, let's suppose we are creating a package called Company\Geo that has the data of countries and continents and flags, etc.例如,假设我们正在创建一个名为Company\Geo的包,其中包含国家、大陆和旗帜等数据。

It's a good UX that this package should have 100% self-contained seeder classes to populate the database tables with the data.这是一个很好的用户体验,这个包应该有 100% 独立的播种器类来用数据填充数据库表。

However, I'm stuck at running seeders from packages.但是,我一直坚持从包中运行播种机。 This is my package directory strcuture:这是我的包目录结构:

- host => (a laravel app to test the package)
   - WebSite
       - packages
           - Company
               - FirstPackage
                   - database
                       - migrations
                       - seeders
                           - FirstSeeder.php
                   - src
                       - Models
                       - Http
                           - Controllers

And here is my FirstSeeder.php code:这是我的FirstSeeder.php代码:

<?php

namespace Company\FirstPackage\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;

class FirstSeeder extends Seeder
{
    public function run()
    {
        // code to seed
    }
}

And here is my composer.json of the Company\FirstPackage package:这是我的Company\FirstPackage包的composer.json

"autoload": {
    "classmap": [
        "/database/migrations"
    ],
    "psr-4": {
        "Company\\FirstPackage\\": "src/",
        "Company\\FirstPackage\\Database\\": "database/"
    }
},

Yet when I run php artisan db:seed --class=FirstSeeder I get this error:然而,当我运行php artisan db:seed --class=FirstSeeder我得到这个错误:

Target class [Database\Seeders\FirstSeeder] does not exist.目标类 [Database\Seeders\FirstSeeder] 不存在。

How can I call seeders of my package, without publishing them to the host package?如何在不将它们发布到主机包的情况下调用包的播种机?

I have just read the source code.我刚刚阅读了源代码。 I believe you got to do this:我相信你必须这样做:

// Pls use double backward slashes.
php artisan db:seed --class=php artisan db:seed --class=Company\\Package\\Database\\Seeder\\FirstSeeder

You might want to take a look at the Illuminate\Database\Console\Seeds\SeedCommand::getSeeder() method.您可能想看看Illuminate\Database\Console\Seeds\SeedCommand::getSeeder()方法。

You can use the same logic that is used in ServiceProvider.您可以使用在 ServiceProvider 中使用的相同逻辑。 This way you can simply run: php artisan db:seed and it should run any seeders you have in your packages.这样你就可以简单地运行:php artisan db:seed 它应该运行你的包中的任何播种机。

 protected function loadSeeders($seed_list){
$this->callAfterResolving(DatabaseSeeder::class, function ($seeder) use ($seed_list) {
            foreach ((array) $seed_list as $path) {
                $seeder->call($seed_list);
                // here goes the code that will print out in console that the migration was succesful
            }
        });
    }

then you can create a list using a function that gets every class name you are using in your package.然后您可以使用一个函数创建一个列表,该函数获取您在包中使用的每个类名。 And put it inside, like this然后把它放在里面,像这样

$seed_list[] = 'Vendor/Name/Directory/ClassName'
 // inside your service provider 
boot()
{
$this->loadSeeders($seed_list)
}

this is just a copy past please see https://laracasts.com/discuss/channels/laravel/best-way-to-load-seeds-and-routes-from-a-package这只是过去的副本,请参阅https://laracasts.com/discuss/channels/laravel/best-way-to-load-seeds-and-routes-from-a-package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM