英文:
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
答案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']);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论