我正在我的软件中开发一个推荐系统。我已经获得了正确的推荐,但我想列出所有推荐某人的用户以及他们在管理端推荐的人数。
这是我的用户模型
public function referrals()
{
return $this->hasMany(Referral::class, 'user_id', 'id');
}
public function referrer()
{
return $this->hasOne(Referral::class, 'referred_by', 'id');
}
注意:referred_by是推荐某人的人,user_id 是被推荐的人
这是我的推荐模型
protected $fillable = ['user_id', 'referred_by', 'status'];
public function user()
{
return $this->belongsTo(User::class);
}
这是我的推荐迁移
Schema::create('referrals', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned()->references('id')->on('users');
$table->integer('referred_by')->unsigned()->references('id')->on('users');
$table->string('status')->nullable();
$table->timestamps();
});
胡说叔叔