0

私のコードでは、各会社はパッケージに属し、パッケージ使用クラス Package extends Model を持っています。パッケージモデル:

protected $fillable =[
    'name',
    'daily_cv_view',
    'monthly_cv_view',
    'per_job_post_cv_view',
    'active_job_post_limit',
    'question_per_job_post_limit',
    'package_lifetime',
    'job_post_lifetime_limit',
    'price'
    ];
public function companies()
{
    return $this->hasMany('App\Models\Company');
}

public function packageUsages()
{
    return $this->hasMany('App\Models\package_usage');
}

会社のモデル

 public function package()
    {
        return $this->belongsTo('App\Models\Package', 'package_id');
    }

パッケージ使用モデル

public function company()
    {
        return $this->belongsTo('App\Models\Company');
    }
    public function package()
    {
        return $this->belongsTo('App\Models\Package');
    }

私のデータベース シーダーでは、作成された会社ごとにパッケージ sage を作成しようとしています。そのため、次のようになります。

 $companies=factory(App\Models\Company::class, 20)->create()->each(function ($company) {
            $company->users()->save(factory(App\Models\User::class)->make());
            $company->events()->save(factory(\App\Models\Event::class)->make());
        });
        foreach ($companies as $company)
            $company->packageUsages()->save(factory(\App\Models\PackageUsage::class)->make([
                'company_id'=>$company->id,
                'package_id'=>$company->first()->package_id
            ]));

そして最後に、私のパッケージ使用法 fctory は次のようになります:

$factory->define(App\Models\PackageUsage::class, function (Faker $faker) {
    $companyId= $faker->randomElement(\App\Models\Company::all()->pluck('id')->toArray());
    $company= App\Models\Company::find($companyId);
    $package=$company->package;
    $dailyCvView=$package->first()->daily_cv_view;
    return [
        'company_id'=>$company,
        'package_id'=>$package,
        'daily_cv_view'=>$faker->numberBetween($min=0 , $max=$dailyCvView ),
        'monthly_cv_view'=>$faker->numberBetween($min=$dailyCvView , $max=$package->monthly_cv_view ),
        'active_job_post_count'=>$company->jobPosts->count(),
        'expiration_date'=>$faker->dateTimeBetween($min='now', $max='+ 30 days')
    ];
});

会社の工場:

$factory->define(\App\Models\Company::class, function (Faker $faker) {
        return [
        'name'=>$faker->company,
        'company_size'=>$faker->randomElement(['10','50','100','200']),
        'slogan'=>'باما کار کنید',
        'website'=>$faker->domainName,
        'logo'=>'/images/companies/avatar',
        'message_title'=>'ما در اینجا.....',
        'message_content'=>$faker->paragraph('10'),
        'main_photo'=>'/images/companies/mainphotos/avatar',
        'about_us'=>$faker->paragraph('10'),
        'why_us'=>$faker->paragraph('10'),
        'recruiting_steps'=>$faker->paragraph('10'),
        'address'=>$faker->address,
        'email'=>$faker->safeEmail,
        'phone_number'=>$faker->numberBetween(10,100),
        'location'=>$faker->city,
            'package_id'=>$faker->randomElement(\App\Models\Package::all()->pluck('id')->toArray())
    ];
});

これを実行するとエラーが返されます:

   Illuminate\Database\QueryException  : SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`balatar`.`package_usages`, CONSTRAINT `package_usages_package_id_foreign` FOREIGN KEY (`package_id`
) REFERENCES `packages` (`id`)) (SQL: insert into `package_usages` (`company_id`, `package_id`, `daily_cv_view`, `monthly_cv_view`, `active_job_post_count`, `expiration_date`, `updated_at`, `created_at`) values (4, 4, 12, 484, 0, 2018-09-16 07:26:11, 2018-08-30
 03:45:22, 2018-08-30 03:45:22))

データベースを確認すると、会社 ID とパッケージ ID が同じであるそれぞれに対して 3 つのパッケージが作成されていることがわかります。4 の両方の ID でビルドしようとすると 4 番目のものを考え、3 つのパッケージのみを挿入すると、外部キーが失敗します。私のコードの問題は何ですか?

4

1 に答える 1