简体   繁体   English

如何在mysql和laravel中实现树状结构

[英]How to implement tree like structure in mysql and laravel

A question struck in my mind for 2 days and wondering whether it is possible to implement this type of tree structure in laravel and MySQL. 我想了两天的问题,想知道是否有可能在laravel和MySQL中实现这种树结构。

在此处输入图片说明

(First, take a look at the image attached. Thanks) (首先,查看附带的图像。谢谢)

Suppose our platform uses refer system, and initially, a user 'A' join. 假设我们的平台使用参照系统,并且最初使用用户“ A”加入。 Now, this user 'A' further refers 3 persons 'B','C','D'. 现在,该用户“ A”还指3个人“ B”,“ C”,“ D”。 Now, the total refer on A is 3 (because it refers 3 persons). 现在,A上的总推荐数为3(因为它引用了3个人)。

Now, let B further refers 'E','F' and 'C' further refers 'G','H', 'I' and 'D' refers 0. So, now refer of each person is "D = 0", "C = 3", "B = 2". 现在,让B进一步引用'E','F'和'C'进一步引用'G','H','I'和'D'引用0。因此,现在每个人的引用是“ D = 0” ,“ C = 3”,“ B = 2”。 and these refers will also add up on "A". 这些引用也将加到“ A”上。 So, it has "A = 8". 因此,它具有“ A = 8”。

Now, 'G' refers 'J', so 'G' gets +1 and 'C' also gets +1 and 'C' is referred by 'A', so 'A' also gets +1. 现在,“ G”表示“ J”,因此“ G”得到+1,而“ C”也得到+1,而“ C”由“ A”表示,因此“ A”也得到+1。 Now, total refer to each person is : "j = 0","G=1","H=0","I=0", "D=0","E=0","f=0","B=2","C=4 (beacuse G refers J also)","A=9(beacuase 9 childers are refered by him)" 现在,每个人所指的总数是:“ j = 0”,“ G = 1”,“ H = 0”,“ I = 0”,“ D = 0”,“ E = 0”,“ f = 0” ,“ B = 2”,“ C = 4(因为G也指J)”,“ A = 9(因为9个孩子由他指代)”

The chain continues until A gets total refer of 40. 链条一直持续到A获得总推荐40。

In simple, if a person refers another person then it will get +1 and it's parent whom he gets refer also get +1 and so on until parent reaches 40, the chain continues. 简单来说,如果一个人推荐另一个人,那么该人将获得+1,而他所推荐的父母也将获得+1,依此类推,直到父母达到40岁为止,这条链将继续。

I know, this is One-Many relationship between a user and refer and we can use a pivot table, but, How can we implement this type of logic. 我知道,这是用户和引荐之间的一对多关系,我们可以使用数据透视表,但是,如何实现这种类型的逻辑。 Give me some hints. 给我一些提示。 Thanks. 谢谢。

I have written out something that should hopefully help you with this, using a while loop. 我已经写了一些东西,希望可以通过while循环来帮助您。

public function totalReferredBy(User $user)
{
    // Initialise the queue to contain only the provided user
    $queue = collect([$user]);

    // This collection will eventually contain all of the "child"/referred users
    $results = collect();

    while ($queue->isNotEmpty() > 0) {
        // Run a where in query to select all the referred users of the users in the queue.
        $referredUsers = User::whereIn('referred_by', $queue->pluck('id'))->get();

        // Merge the referredUsers we have found in the database with the results collection, so we can later count.
        $results = $results->merge($referredUsers);

        // Make the referredUsers we have just found in the database, the new queue. If the query did not return any
        // referred users, the queue count would be 0 and the loop will exit.
        $queue = $referredUsers;
    }

    // Now we should have all of the given user's "children" and "children of children" in the $results collection.
    // We just need to return the count of that collection to get the total number of users that have been referred.
    return $results->count();
}

You can use it like this: 您可以像这样使用它:

$user = User::find(1);

$totalReferred = $this->totalReferredBy($user);

Then if your application does something when the user reaches 40 or more referred, you can just do: 然后,如果您的应用程序在用户达到40个或更多推荐人数时执行了某些操作,则可以执行以下操作:

if ($this->totalReferredBy($user) > 40) {
    // Do something
}

This assumes that you have a referred_by column on the users table. 假设您在users表上有一个referred_by列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM