I need to pull the field from a related table of many many when I show in a view.
I have two related tables from many to many, one is: cliente
and the other is servicio
. These are my models:
Service table model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Modelo_servicio extends Model
{
protected $table='servicios';
protected $fillable=[
'descripcion_servicio',
'consumo_agua',
'pago_mantenimiento',
'pago_desague',
];
public function modelocliente(){
return $this->belongsToMany('\App\Modelo_cliente');
}
}
Customer table model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Modelo_cliente extends Model
{
protected $table = 'clientes';
protected $fillable = [
'nombre',
'apellido',
'dni',
'direccion',
'asentamientos_id',
'servicio',
'condicion',
'mz',
'lt',
];
public function Servicio()
{
return $this->belongsToMany('\App\Modelo_servicio');
}
public function asentamiento()
{
return $this->belongsto('\App\Modelo_asentamiento', 'asentamientos_id');
}
}
Table pivot
of these two tables is as follows:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class modelo_cliente_servicio extends Model
{
protected $table = 'cliete_servicio';
protected $primarykey='id';
protected $fillable = [
'clietes_id',
'servicios_id',
];
}
now when I want to show a data from the service table in the view :
@foreach($cliente as $client)
<tr>
<td>
{{ $client->nombre }}
</td>
<td>
{{ $client->apellido }}
</td>
<td>
<strong>
{{ $client->dni }}
</strong>
</td>
<td>
<strong>
{{ $client->direccion }}
</strong>
</td>
<td>
<strong>
{{ $client->asentamiento->nombre }}
</strong>
</td>
<td>
<strong>
{{ $client->Servicio->descripcion_servicio}}//aqui muestro
</strong>
</td>
<td>
does not show me anything and I get the following exception:
The pivot table of these two tables is as follows:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class modelo_cliente_servicio extends Model
{
protected $table = 'cliete_servicio';
protected $primarykey='id';
protected $fillable = [
'clietes_id',
'servicios_id',
];
}