为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 7 建立联系以找出邀请我的人

Laravel 7 建立联系以找出邀请我的人

PHP
白衣非少年 2023-04-28 14:22:44
在我的网络应用程序中,每个应用程序都可以像网络系统一样拥有无限的用户。我可以找到哪个用户注册了用户参考码,但我想知道谁是我的父母并邀请我注册例如,此代码可以返回有多少用户使用我的参考代码注册:$users = User::whereNull('user_id')->with('child')->get();模型:public function child(){    return $this->hasMany(User::class)->with('child');}我怎样才能知道谁是孩子的父母?就像从树枝到根User迁移文件:Schema::create('users', function (Blueprint $table) {    $table->id();    $table->string('name')->nullable();    $table->string('family')->nullable();    $table->string('username')->unique();    $table->string('email')->unique();    $table->timestamp('email_verified_at')->index()->nullable();    $table->unsignedBigInteger('user_id')->index()->nullable();    $table->foreign('user_id')->references('id')->on('users');    $table->string('password');    $table->rememberToken();    $table->softDeletes();    $table->timestamp('created_at')->useCurrent();    $table->timestamp('updated_at')->useCurrent();});User模型:class User extends Authenticatable{    use Notifiable, SoftDeletes;    protected $guarded = [        'id',    ];    protected $hidden = [        'password', 'remember_token',    ];    protected $casts = [        'email_verified_at' => 'datetime'    ];    public function users()    {        return $this->hasMany(User::class)->with('users');    }}
查看完整描述

2 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

为了得到父母

{
    return $this->belongsTo(User::class, 'user_id');
}

为了让孩子们

{
    return $this->hasMany(User::class, 'user_id', 'id');
}



查看完整回答
反对 回复 2023-04-28
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

此解决方案工作正常:


public function child()

{

    return $this->hasMany(User::class)->with('child');

}


public function parent()

{

    return $this->belongsTo(User::class, 'user_id');

}


查看完整回答
反对 回复 2023-04-28
  • 2 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信