Consult children objects that satisfy condition in parent objects, Relationship with eloquent

Asked

Viewed 328 times

-3

I have in my project two tables that relate, but I would like to make a query Consult that brings all children entities that a field is equal to parent entity, being for all parent entities that satisfy a specific condition.

Someone could help me?

I want to take all the wheels that vehicle (parent class) is red, being that vehicles is a table, wheels is another, and color (the red) is a field. Dummy example. In this case I want all wheels only.

  • 3

    "bring all child entities that a field is equal to parent entity" You want a field to be the same to the whole entity? Please try to be a little clearer.

  • Sorry for the confusion, I want to take all the vehicles that contain chrome wheel, being that vehicles is a table, wheels is another, and wheel type (the chrome) is a field. Fictional example

  • 1

    Young man, realize there is no absolutely no relationship between what you ask in your question and what you say in the previous comment. Please click on [Edit] and try to explain your problem better and give an example of what you want to do.

  • Sorry, I explained really confused, I corrected the explanation of what I want editing the post, is a problem that has taken my sleep.

1 answer

3


Since you didn’t give a real example, I used the fictional classes Pai and Filho

$dados = Pai::with('filhos')->where('coluna', 'algum valor')->get();

In this case, it is necessary to have the relationship mapped in the model

class Pai extends Model
{
    public function filhos() {
        return $this->hasMany(Filho::class);
    }
}

Applying to the example of Veiculo and Roda

class Veiculo extends Model 
{
    public function rodas() {
        return $this->hasMany(Roda::class);
    }
}

The use would be

$dados = Veiculo::with('rodas')->where('cor', 'vermelho')->get();

To return only the daughter entities, it is possible to make a join

$dados = Roda::join('veiculos', function($query) {
    $query->on('veiculos.id', '=', 'rodas.veiculo_id');
    $query->where('veiculo.cor', '=', 'vermelho');
})
->get();
  • The example I need is different from this, I basically want all the children, in the case wheels that satisfy a father’s condition, that’s where it gets a little harder because I don’t know how to do this.

  • 1

    You want all the wheels where the id_veiculo is equal to id of a vehicle? @Andersoneyrodrigues

  • Exactly. But not from a vehicle, but from all vehicles that satisfy the condition

  • @Andersoneyrodrigues See my edition

  • But in this case I don’t want to take the parent classes, I only want the child within the condition.

  • There is no way your condition can be performed by bringing only wheels and no vehicles containing wheels within the object?

  • @Andersoneyrodrigues See the final part of the answer

  • Is there any way to remove some fields in the return of the given object? For example, there are several fields that I do not want to be returned in this query.

  • There is. You can ask another question about this.

  • Created, if you can help me I’m grateful

Show 5 more comments

Browser other questions tagged

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