正如你们所看到的Image Above,我想创建用户可以注册和输入referral_user的推荐系统affiliate users。
并且Referral_Code对每个用户都是独一无二的。
My Problem is I cant track Whom that code is.
My User Schema
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('referrer_id')->nullable();
$table->foreign('referrer_id')->references('id')->on('users');
$table->string('referral_code')->unique()->nullable();
$table->string('referred_by')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
My User Model
public function referrer()
{
return $this->belongsTo(User::class);
}
public function referrals()
{
return $this->hasMany(User::class);
}
In My UserController
$referrer = User::where('name', auth()->user()->name)->first();
$user = new User();
$user->name = $request->name;
$user->referral_code = substr(uniqid(), 0, 8); // for unique id
$user->email = $request->email;
$user->referrer_id = $referrer ? $referrer->id : null;
$user->role = $request->role;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'created' => true,
]);
希望你们能给我一些想法,如果你们发现我的代码有错误,请纠正我,我会感谢你们的帮助。
谢谢...
qq_笑_17