4

如何使用来自同一个表的两个外键为表播种,例如消息:

移民:

    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('to')->unsigned();
            $table->integer('from')->unsigned();
            $table->integer('parent_id')->unsigned()->nullable();
            $table->text('body');
            $table->boolean('status')->default(false);
            $table->timestamps();
        });

        Schema::table('messages', function (Blueprint $table) {
            $table->foreign('to')
                    ->references('id')->on('users')
                    ->onDelete('cascade');

            $table->foreign('from')
                    ->references('id')->on('users')
                    ->onDelete('cascade');

            $table->foreign('parent_id')
                    ->references('id')->on('messages');

        });
    }

模型工厂:

$factory->define(App\Message::class, function($faker) {

    return [
        'body' => $faker->text
        'from' => //user id,
        'to' => //user id,
        'parent_id' => //message id
    ];

});
  1. 我如何在这里获取用户 ID?
  2. 如何获取 id 的现有消息行?
4

2 回答 2

2
$messages = factory(App\Message::class, 20)->create();
$messageIds = DB::table('messages')->lists('id'); // returns array of userids
DB::table('categories')->where('id', $rowid); // where $rowid is the id which you want

完整的解决方案是这样的

class MessagesTableSeeder extends Seeder
{
    public function run()
    {
        // start with some cleanup    
        DB::table('messages')->truncate();
        // Create some App\Message instances...
        $messageCount = 30;
        $messages = factory(App\Message::class, $messageCount)->create();
        $messageIds = DB::table('messages')->lists('id');
        for($i=0; $i<$messageCount - 1; $i++) {
            $randomizedMessageIds = $messageIds;
            shuffle($randomizedMessageIds);
            DB::table('messages')->where('id', $i)->update(['parent_id'=>$randomizedMessageIds[$i]]);
        }
    }
}
于 2016-06-08T19:00:36.963 回答
0

聚会迟到了,但我有另一个解决方案:

在为任何模型创建工厂时,前几次,让 parent_id 为 0。在数据库中创建指定的条目后,您可以传入已经创建的实际获取的数据。

我用它来自我引用类别和子类别。

 $factory->define(CMS\Models\Category::class, function (Faker\Generator $faker) {
    $users = \CMS\Models\User::all()->pluck('user_id')->toArray();

    /* Get all categories, if there are less then 4, the reference 
    is an array with a 0, meaning no parent. After 4 are created, we 
    start picking random categories s a parent ID, the list will grow 
    so does the variation. Of course you can set the minimum amount 
    of created categories as you wish.*/

    $categories = \CMS\Models\Category::all()->pluck('category_id');
    if (count($categories) <= 4) {
        $categories = [0];
    } else {
        $categories = $categories->toArray();
    }
    return [
        'category_id' => $faker->unique()->numberBetween(1,1000),
        'title' => $faker->sentence(1,40),
        'description' => $faker->text(50),
        'content' => $faker->paragraph(rand(3,5)),
        'approved' => $faker->boolean(),
        'trashed' => $faker->boolean(),
        'type' => $faker->randomElement('post','product'),
        'user_id' => $faker->randomElement($users),
        'parent_id' => $faker->randomElement($categories),
        'created_at' => $faker->dateTimeThisYear,
        'updated_at' => $faker->dateTimeThisYear,
    ];
});

count($categories) <= 1使用此方法,如果您设置将 parent_id 添加到数据库所需的条目,您将至少有 1 个没有父条目。如果绝对需要让所有条目都具有 parent_id,则只需手动更改数据库中的 1 列。

于 2017-09-01T12:53:46.420 回答