Laravel 8:如何在多对多关系中为数据透视表种子

Laravel 8:如何在多对多关系中为数据透视表种子,laravel,laravel-seeding,Laravel,Laravel Seeding,我想通过播种机/工厂为我的桌子播种。 我想创建一个透视表,它连接表atg和debtor,名为atg\u debtor,它有一个id,在处创建,在处更新,atg\u id,债务人id 通过雄辩插入数据时,一切正常。当我尝试使用Seeders/Factory为我的数据库添加种子时,它总是尝试将数据插入我的atg表,这是我不想要的。我想为债务人表(工作正常)和具有给定atg_id和动态债务人_id的透视表种子,这是在为债务人种子设定时创建的 我的债务人模式: <?php namespace A

我想通过播种机/工厂为我的桌子播种。 我想创建一个透视表,它连接表
atg
debtor
,名为
atg\u debtor
,它有一个
id
处创建,
处更新,
atg\u id
债务人id

通过雄辩插入数据时,一切正常。当我尝试使用Seeders/Factory为我的数据库添加种子时,它总是尝试将数据插入我的
atg
表,这是我不想要的。我想为
债务人
表(工作正常)和具有给定
atg_id
和动态
债务人_id
的透视表种子,这是在为债务人种子设定时创建的

我的债务人模式:

<?php

namespace App\Models;

use App\Models\Atg;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Debtor extends Model
{
    use HasFactory;

    public function atg()
    {
        return $this->belongsToMany(Atg::class)->withTimestamps();
    }
}

所以首先你可以检查数据库中是否有
Atg
,如果没有,你可以像你一样播种,否则你可以播种
债务人
,然后在关系中保存
Atg
->Atg()
是你的关系名,所以如果我写错了名字,就改进它)这样:

另外,我从
债务人
中删除了
->count(1)
,因为默认情况下它创建一个项目

<?php

namespace Database\Seeders;

use App\Models\Atg;
use App\Models\Debtor;
use Illuminate\Database\Seeder;

class DebtorSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $atg = Atg::first();
        if($atg){
           $debitor = Debtor::factory()
                ->create();

           $debitor->atg()->save($atg);
        }else {
           $debitor = Debtor::factory()
                ->hasAtg(1)
                ->create();
        }
    }
}

假设您要单独设置数据库表的种子:

// Seed 50 Atg records
Atg::factory(50)->create();

// Seed 4 Debtor records
Debtor::factory(4)->create();
您可以在
债务人
工厂中覆盖
配置
方法,以便在创建一些
Atg
模型后附加它们:

class DebtorFactory extends Factory
{
    public function configure()
    {
        return $this->afterCreating(function (Debtor $debtor) {
            // Once a Debtor record has been created
            // Pick between 1 and 5 Atg records in a random order
            // Associate the Atg and Debtor records
            $debtor->atg()
                   ->attach(Atg::inRandomOrder()->take(random_int(1, 5))->pluck('id'));
        });
    }
}

谢谢这对1插入非常有效。如果我想用->计数(10)来设定f/e 10的种子,那么它就不起作用了。你能解释一下如何播种超过1个债务人吗?错误:BadMethodCallException方法Illumb\Database\Eloquent\Collection::atg不存在。您可以共享所有代码以使其更清晰吗?我获取了您的代码,该代码运行良好(再次感谢),并在->create();->计数(10)。然后它抛出发布的错误好吧,我理解这个问题,因此在这种情况下,它将返回一个集合实例,而不是模型实例,因此如果您想插入许多实例,您需要这样做,抱歉格式错误
$debitors=Debtor::factory()->count(10)->create();foreach($debitors as$debitor){$debitor->atg()->save($atg);}
这是否有效@皮祖库
// Seed 50 Atg records
Atg::factory(50)->create();

// Seed 4 Debtor records
Debtor::factory(4)->create();
class DebtorFactory extends Factory
{
    public function configure()
    {
        return $this->afterCreating(function (Debtor $debtor) {
            // Once a Debtor record has been created
            // Pick between 1 and 5 Atg records in a random order
            // Associate the Atg and Debtor records
            $debtor->atg()
                   ->attach(Atg::inRandomOrder()->take(random_int(1, 5))->pluck('id'));
        });
    }
}