亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超8個贊

為了得到父母

{
    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 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號