0
I have, briefly, the following structure::
Suppliers: id | cnpj | razao_social | endereco_id
People: id | nome | tipo_pessoa | fornecedor_id
Addressee: id | logradouro | cep
Model - Supplier:
public function endereco() {
return $this->hasOne('App\Endereco', 'id', 'endereco_id');
}
public function pessoa() {
return $this->hasMany('App\Pessoa');
}
Model - Pessoa:
public function fornecedor() {
return $this->belongsTo('App\Fornecedor', 'id', 'fornecedor_id');
}
Model - Address:
public function fornecedor() {
return $this->belongsTo('App\Fornecedor');
}
Vendorcontroller:
public function show($id) {
$fornecedor = Fornecedor::find($id)->with('endereco', 'pessoa')->get();
return view('admin.fornecedores.visualizar', compact('fornecedor'));
}
I want to show a supplier, his address and the people related to it. The query in my controller
works, however, I want the consultation to bring only people who have tipo_pessoa
= CF
or RF
How can I use something like 'WHERE
' to do this in consultation?
I used it on the model and it worked perfectly. Thanks again, @juniorb2ss!
– buback
@Buback only that in the model it will impose this rule for ALL relationship. Do not forget to mark the answer as the correct one.
– juniorb2ss
Okay, I’m going to test both ways and see what impact I want. Thank you very much.
– buback
another question: how do I bring city and state that are also in their respective tables?
– buback
How so? You want to bring the city the supplier belongs together with the state the city belongs to?
– juniorb2ss
That, in my model supplier I only refer the Address, as I can do for the address search the city and the state. In the address table has city_id and in the city table has stay_id.
– buback
Just create a relationship.
endereco belonsTo cidade
andcidade belongsTo estado
. Then just recover with $addressee->city->name; or $addressee->city->state->name;– juniorb2ss
Let’s go continue this discussion in chat.
– buback