【问题标题】:laravel factory insert foreign keylaravel 工厂插入外键
【发布时间】:2016-11-28 17:48:32
【问题描述】:

大家好,想帮忙的人

我试图创建工厂文件来播种我的数据库,我有一个问题,我如何从已经播种的表中插入外键? 并且工厂代码要全部放在同一个文件中?有什么好的做法吗?

文件

模型用户

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

  protected $table = 'user'; //name of the table in database
  protected $primaryKey = 'Id'; //Primary Key of the table

  /**
   * Relations between tables
   */
   public function GetLoginInfo()
   {
     return $this->hasMany('App\Models\LoginInfo', 'UserId');
   }

   public function getStatus()
   {
     return $this->belongsTo('App\Models\AccountStatus');
   }

}

模型帐户状态

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AccountStatus extends Model
{
  protected $table = 'account_status'; //name of the table in database
  protected $primaryKey = 'Id'; //primary Key of the table
  public $timestamps = false; //true if this table have timestaps
  /**
   * Relations between tables
   */
   public function GetUsers()
   {
     return $this->hasMany('App\Models\Users', 'StatusId');
   }
}

工厂文件:

<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */

//Factory for Account Status table
$factory->define(App\Models\AccountStatus::class, function (Faker\Generator $faker) {
    return [
      'Description' => $faker->word,
    ];
});

//Factory for user table
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
    return [
      'Username' => $faker->unique()->userName,
      'Password' => bcrypt('test'),
      'Email' => $faker->unique()->safeEmail,
      'Name' => $faker->name,
      'StatusId' => Factory(App\Models\AccountStatus::class)->create()->id,
    ];
});

正如您所见,这就是我试图做的事情:Factory(App\Models\AccountStatus::class)->create()->id but don't work

【问题讨论】:

    标签: php laravel laravel-5.3


    【解决方案1】:
    $factory->define(App\Models\User::class, function (Faker\Generator $faker) {
        return [
          'Username' => $faker->unique()->userName,
          'Password' => bcrypt('test'),
          'Email' => $faker->unique()->safeEmail,
          'Name' => $faker->name,
          'StatusId' => factory(App\Models\AccountStatus::class)->create()->id,
        ];
    });
    

    我在工厂看到一个大写的 F..

    $factory->define(App\Models\User::class, function (Faker\Generator $faker) {
    
         $accountStatus = factory(App\Models\AccountStatus::class)->create()
    
         return [
          'Username' => $faker->unique()->userName,
          'Password' => bcrypt('test'),
          'Email' => $faker->unique()->safeEmail,
          'Name' => $faker->name,
          'StatusId' => $accountStatus->id,
        ];
    });
    

    编辑(改进)

    如果您有一个模型依赖于另一个模型。你可以这样做,使用回调函数来创建相关的。

    这样

    $factory->define(App\Models\User::class, function (Faker\Generator $faker) {
        return [
          'Username' => $faker->unique()->userName,
          'Password' => bcrypt('test'),
          'Email' => $faker->unique()->safeEmail,
          'Name' => $faker->name,
          'StatusId' => function () {
              return factory(App\Models\AccountStatus::class)->create()->id;
           }
        ];
    });
    

    您需要记住的一件事是,如果相关(状态模型)具有依赖于父(用户模型)的模型,这将进入无限循环。

    【讨论】:

    • 工作,但我找到了另一种方法,这样我总是创建一个新的帐户状态,这样我就可以得到一个已经为我的示例创建的随机更好的 'StatusId' => \App\Models\AccountStatus:: all()->random()->Id,
    猜你喜欢
    • 2021-04-25
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2019-06-21
    • 2018-04-18
    • 2019-07-11
    相关资源
    最近更新 更多