如何使用来自同一个表的两个外键为表播种,例如消息:
移民:
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
];
});
- 我如何在这里获取用户 ID?
- 如何获取 id 的现有消息行?