Laravel Recursion?

0

Next, in my model :

public function indicacao()
{
    return $this->hasMany(User::class, 'indicacao','name');
}

public function todasindicacoes()
{
    return $this->indicacao()->with('todasindicacoes');
}

and my controller so

public function rede()
{

    $Usuarios = User::where('name', 'admin')->with('todasindicacoes')->get();
    //dd($Usuarios);
    $cont = 1;
    foreach($Usuarios as $usuario){

     $x =  $usuario->todasindicacoes;
     echo $x;

}

At the end of the day this returns me a result and that search and other result and so infimamente, I would like to know and if you have how many times and this has run, because in the bank I have

|login    | indicacao |
|---------|-----------|
|matheus  | null      |
|fulano   | matheus   |
|beltrano | matheus   |
|ciclano  | beltrano  |
  • matheus is level 0

  • so and so beltran is level 1

  • and cyclane is level 2

asked by anonymous 02.01.2018 / 22:56

1 answer

0

Logic to solve by example

For your example you will have to create a logic where, for example, usuario is ciclano , in the index of the table where the ciclano is checked if indicacao != null

If it is different from null you should store the name contained in indicacao in a temporary variable, and in another variable add 1

You should then look in the login column for the name stored in the variable, in the column indicacao in the index of the line where the name is contained if it is not null then save the new name in the variable and add 1 in the other variable

Make this loop until indicacao checks equal null

Then you will have in usuario the name of login and in that variable where you added 1 the level of this

Best way to solve

But a much simpler way to resolve this is to add a column containing the user level to this table

I do not know exactly the logic of your application for this level, but probably your registration is linked to the indication, so at the time of registration you just have to check the level of who indicated and add 1 (if there is no indication is level 0)

|login    | indicacao | nivel |
|---------|-----------|-------|
|matheus  | null      | 0     |
|fulano   | matheus   | 1     |
|beltrano | matheus   | 1     |
|ciclano  | beltrano  | 2     |

So when you want to know the level of a user, just search the table

    
03.01.2018 / 04:44