我怎樣才能播種表與來自同一個表像的消息兩個外鍵:如何種子自參照表Laravel 5.1
遷移:
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');
});
}
ModelFactory:
$factory->define(App\Message::class, function($faker) {
return [
'body' => $faker->text
'from' => //user id,
'to' => //user id,
'parent_id' => //message id
];
});
- 我該如何獲得用戶ID?
- 如何獲取id的現有消息行?
快速注意列表已在5.3中被棄用,以支持pluck,所以它變成:$ messageIds = DB :: table('messages') - > pluck('id');它返回一個集合。爲了將數組放入$ randomizedMessageIds進行混洗,我們需要:$ randomizedMessageIds = $ messageIds-> all(); – theotherdy