laravel - 如何在此二进制推荐系统中插入位置 ID
问题描述
我创建了带有推荐链接的用户注册系统。新用户可以轻松地在此推荐 ID 下注册,我将新用户设置为左右位置。但我仍然需要重点是在用户表中添加位置id,因为二叉树系统,新用户将放置在左右树中。因此,如果现有用户在他前面,则该位置将在现有用户的下方。为此,我想设置位置 id 来计算和排序这个二叉树用户。
这是我的寄存器控制器
protected function create(array $data)
{
$referred_by = Cookie::get('referral');
return $user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'position' => $data['position'],
'password' => Hash::make($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $referred_by,
//'position_id' => ?
]);
}
这是我的用户表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('position');
$table->string('email')->unique();
// $table->string('posid');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('parent_id')->nullable(); // root user will have null parent_id
$table->string('name');
$table->string('position');
$table->string('email')->unique();
$table->string('password');
$table->string('posid');
$table->rememberToken();
$table->timestamps();
$table->foreign('parent_id')->references('id')->on('users')->onDelete('cascade'); // cascade delete children when deleting parent
});
如何编辑 registercontroller 以在数据库中获取此位置 ID。我可以为在系统中注册的每个用户 ID 使用 parent_id。怎么能用。
请帮我。