Good morning, I am iterating some records in a view that are the clients, this iteration is already connected to a relationship that in this case takes me all the products that this client has, these products are real estate and have a Province and a Municipality and here it is where I find the problem.
living place
go | yam | municipality | Province | client_id |
---|---|---|---|---|
1 | Renovated with parking space included | 350 | 598 | 1 |
two | Renovated with elevator | 355 | 598 | 1 |
Province
go | Province |
---|---|
598 | Valencia |
620 | Barcelona |
Municipality
go | province_id | municipality |
---|---|---|
350 | 598 | alfafar |
355 | 598 | sedavi |
Client
go | Name |
---|---|
1 | Marian |
two | Roberta |
So first I iterate my entire customers table
$clientes = Cliente::with('inmuCliente')->get(); //Aquí ya le estoy añadiendo la relación de las propiedades que tiene este cliente con inmuCliente
@foreach ($clientes as $item)
....
....
//Aquí itero las propiedades que tiene el cliente
@forelse ($item->inmuCliente as $prop)
<h5>//Aquí tiene que ir la Provincia y el mmunicipio</h5>
@empty
<div class="alert alert-warning editalert" role="alert" style="font-size: 13px;">
No tiene inmuebles
</div>
@endforelse
@endforeach
my client model
protected $primaryKey='id';
protected $table = 'cliente';
public function inmuCliente()
{
return $this->hasMany(Vivienda::class, 'cliente_id');
}
So I don't know how to make me put the name of the Province and the Municipality.
I think the error is in the forelse in which the forelse should be changed to a foreach
you can use the var_dump(); to print the entire array flat or output as json, it is recommended to use the https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa extension
In the view you must show parameters not relations, that is to say that in the model the relation is inmuCliente but if you want to access this relation from the view, the CamelCase automatically converts to camel_case , that is, inmu_cliente , in fact in the latest versions it is not necessary to call the with method to be able to load them in the blade, only by calling the relation you can get the data, if what you want is to send the data to another side that is not a view there if you use the laod() method