What’s the matter with you?

Asked

Viewed 329 times

-1

Next, on my model:

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

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

and my controller thus

public function rede()
{

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

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

}

in the end it returns me a result and that search and other result and so infidelity, I would like to know and if it has as many times as it ran, Yeah, I got this on the bench

|login    | indicacao |
|---------|-----------|
|matheus  | null      |
|fulano   | matheus   |
|beltrano | matheus   |
|ciclano  | beltrano  |

  • Matheus is level 0

  • fulano and beltrano is level 1

  • and Ciclano is level 2

  • You want to know how many times the query was executed to return this data?

  • n exactly, I want to know, in q level so-and-so is, in the case of the example beltrano ta no 2

  • ->with('todasindicacoes') this is not correct for this particular case, it is not a scope that you need to create?

  • Dude, I’m still new to Latin, but in this case it’s working just the way I want it, good almost rsrsrs

1 answer

-1

Logic to solve by example

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

If it differs from null vc should store the name contained in indicacao in a temporary variable, and in another variable sum 1

Then you should search the column login by the name stored in the variable, in the column indicacao in the index of the row where the name is contained if it is not null then stores the new name in the variable and sum 1 in the other variable

Performs this loop until the verification of the indicacao be equal to null

Then you’ll have in usuario the name of login and in that variable in which it added 1 the level of this

Best way to solve

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

I do not know exactly what the logic of your application for this level, but probably your registration is linked to indication, so at the time of registration just consult the level of who indicated and add 1 (if you do not have 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 some user, just search in the table

  • nice face this q vc talando, but would not have as because the so-and-so would have level 1 tb, because the same would have access to this report is like a pyramid

  • @Matheusgonzales then explain better, because what he implied when registering the fulano you would have in the form in the case of matheus, when consulting the level of matheus this is level 0 and in this case the level of fulano would be 0+1 which would give so-and-so a level 1 as you described in the question

  • you put fulano e beltrano é nivel 1 what gives exactly what I described

  • I was a little unhappy in the description actually, but when the so-and-so enters the system, it has a network, where the so-and-so is the top, it’s weird, but it’s like this, it’s a multilevel thing

  • @Matheusgonzales ta but then what is the level of fulano? would be 0?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.