代码之家  ›  专栏  ›  技术社区  ›  Vahid Saberi

厂房返回公司id和包装id相同

  •  0
  • Vahid Saberi  · 技术社区  · 8 年前

    在我的代码中,每个公司都属于一个包,并且有一个包的用法

    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');
        }
    

    packageUsage模型

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

    在我的数据库种子,我试图建立一个软件包为每个公司创建。所以它看起来是这样的:

     $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
                ]));
    

    最后,我的包装用法是这样的:

    $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))
    

    当我检查数据库时,我看到它为每个包创建了3个包,公司id和包id是相同的。因为我认为对于第四个,当它试图用4个id构建它时,我只插入了3个包,一个外键失败了。 我的代码有什么问题?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Mohammad Nourinik    8 年前

    我认为你的第一个错误是在数据库设计上。贵公司将有多个软件包,据我所知,当时只有一个有效的软件包。所以它需要在你的 companies packages

    Schema::create('rel_company_package', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->unsignedInteger('package_id');
        $table->unsignedInteger('company_id');
        $table->timestamp('activated_at')->nullable();
        $table->timestamp('expires_at')->nullable();
        $table->boolean('is_active')->nullable();
        $table->unsignedInteger('nubmer_of_remaining_job_posts');
    });
    
    1. 激活日期:贵公司激活其软件包的日期。
    2. 剩余职位的数量:首先来自你的数据包,一旦你的公司发布了一个职位,就会减少一个。

    注:请填写 activated_at , expires_at is_active 一旦决定激活包,就立即使用正确的值。

    你也应该填写 nubmer_of_remaining_job_posts

    你决定播种的是生产时间上的日志数据,你可能不依赖它,因为它很容易变得庞大,需要大量的资源来计算你想要的。顺便说一句,日志数据( PackageUsage )应该保持你的数据透视表( rel_company_package ) id .

    让我们直接回到您的错误:

    首先在你的 company factory package usage factory 是),最好不要这样做,而是让MySQL为您随机挑选一个

    $company = $\App\Models\Company::query()->inRandomOrder->first();
    // and use $company->id
    

    对于随机包装:

    $package = \App\Models\Package::query()->inRandomOrder()->frist();
    // and use $package->id
    

    包装使用 实例如下:

    $company->packageUsages()->save(factory(\App\Models\PackageUsage::class)->make([
        'package_id' => $company->first()->package_id
    ]));
    

    company_id 再一次。你好像错过了比赛 packageUsages() 方法来自 Company 模型,它应该像:

    public function packagesUsage() 
    {
        return $this->hasMany('\App\Models\PackageUsage', 'company_id')
    }
    

    可能是你换的打字错误 comapny_id 具有 package_id .

    但正如我一开始提到的,你最好先修好你的结构。

    推荐文章