i have a MLM system. where User hasmany Users. now i want to get all users of root/ node A user. where one user hasmany user (in deep) laravel

huangapple go评论71阅读模式
英文:

i have a MLM system. where User hasmany Users. now i want to get all users of root/ node A user. where one user hasmany user (in deep) laravel

问题

I have a MLM system where a User has many Users. Now, I want to retrieve all users of the root/level-1 user, where each user can have many child users, and these child users can have their own child users (in-depth) in Laravel.

Here's the relationship in the model:

// All Child Referrals
public function referral_child_users()
{
    return $this->hasMany(User::class, 'ref_by', 'id')->select('id', 'username', 'ref_by');
}

// All Child Referrals with Multilevel Child Users
public function referral_multilevel_child_users()
{
    return $this->referral_child_users()->with('referral_multilevel_child_users');
}

In the controller:

$user = User::with('referral_multilevel_child_users')->select('id', 'username', 'ref_by')->find(Auth::user()->id);

However, you mentioned that you encountered an error: "Undefined array key 0." If you need assistance with this error, please provide more details about the issue, and I'll be happy to help.

英文:

i have a MLM system. where User hasmany Users. now i want to get all users of root/ lavel-1 user and where one user hasmany user and each of the users can hasmany user (in deep) laravel.

Here Is relation in model:

    //    All Child Refer
    public function referral_child_users()
    {
        return $this->hasMany(User::class, 'ref_by', 'id')->select('id', 'username', 'ref_by');[!
    }

    //    All Child Refer
    public function referral_multilevel_child_users()
    {
        return $this->referral_child_users()
        ->with('referral_multilevel_child_users');
    }

in Controller:

$user = User::with('referral_multilevel_child_users')->select('id', 'username', 'ref_by')->find(Auth::user()->id);

then i got an error: Undefined array key 0

for example: i want all count of users whose prime parent is A

i have a MLM system. where User hasmany Users. now i want to get all users of root/ node A user. where one user hasmany user (in deep) laravel

答案1

得分: 0

似乎您使用查询来获取数据的方式导致了结果

请查看以下内容:

$user = \Auth::user();
$users = User::where('ref_by', $user->id)
    ->orWhereHas('referral_multilevel_child_users', function ($query) use ($user) {
        $query->where('ref_by', $user->id);
    })
    ->get();
英文:

It seems that the way you use the query to fetch data cause the result

Check this out:

$user = \Auth::user();
        $users = User::where('ref_by', $user->id)
            ->orWhereHas('referral_multilevel_child_users', function ($query) use ($user) {
                $query->where('ref_by', $user->id);
            })
            ->get();

答案2

得分: 0

Sure, here is the translated code part:

// 所有子级推荐
public function referral_multilevel_child_users()
{
    return $this->referral_child_users()
        ->with(['referral_multilevel_child_users']);
}

Please let me know if you need any further assistance.

英文:

Change the with function ->with(['referral_multilevel_child_users']);

    //    All Child Refer
    public function referral_multilevel_child_users()
    {
        return $this->referral_child_users()
        ->with(['referral_multilevel_child_users']);
    }

huangapple
  • 本文由 发表于 2023年4月17日 03:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定