I am working on a tree structure relationship - backwards. Meaning, I have users in the database who are referred to the site by other users and I need to retrieve upper referrers up to 10 levels high.
I am using anonymous function inside of my function for this but it says "Maximum function nesting level of '100' reached, aborting!" What am I doing wrong here?
Here is my code:
public function showSponsors($user_id, $count = 10)
{
//The array to store referrers
$Referrers = array();
//The anonymous function that uses Laravel Eloquent model to retrieve data
$GetReferrers = function($id) use(&$Referrers, &$count, &$GetReferrers)
{
//Get the first referrer and store id as index and name as value
$Referrers[$upper = User::find($id)->referrer] = User::find($upper)->Name;
//Make sure you do not return more than $count
//Make sure the referrer returned has a referrer to return
if(count($Referrers) < $count || !empty($upper || $upper != $user_id)){
$count--; return $GetReferrers($upper);
}
//Otherwise return the referrers;
return $Referrers;
};
//Run the function using the user id
$GetReferrers($user_id);
//return the results stored in the Referrers array
return $Referrers;