【发布时间】:2020-08-01 00:23:41
【问题描述】:
我正在我的软件中开发推荐系统。我已获得推荐权,但我想列出 auth 用户推荐的所有用户。
注意:我正在编写一个 API 端点,所以我不能使用关系来显示他们的名字。 这是我的用户模型
public function referrals()
{
return $this->hasMany(Referral::class, 'user_id', 'id');
}
public function referrer()
{
return $this->hasOne(Referral::class, 'referred_by', 'id');
}
注意:refered_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();
});
【问题讨论】:
-
您为什么不直接编辑
users 表并添加referred_by和status?我对你的referrals表结构感到很困惑
标签: laravel