Call the field of a related table from many to many

0

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',

    ];
}
    
asked by Frank Kevin Machacuay 24.03.2018 в 19:20
source

1 answer

0

When the conventions are not followed you have to indicate the name of the pivot table.
 In your model you should indicate it in the following way (in the 2 models).

public function Servicio()
{
    return $this->belongsToMany('\App\Modelo_servicio', 'cliete_servicio', 'sevicios_id');
}

Is the name of the table in the modelo_cliente_servicio model correct? Maybe the table is called cliente_servicio and not cliete_servicio

Here you have the documentation: eloquent-relationships # many-to-many

Regarding the Customer Model Model in the asentamiento() method the relationship is called belongsTo() and you put belongsto .
Try this

public function asentamiento()
{
    return $this->belongsTo(Modelo_asentamiento::class, 'asentamientos_id');
}

Modifying this method when doing $client->asentamiento->nombre should work.

If you are just starting the project maybe you could rename the tables and models so you will be simplified to follow the documentation.

You could do it like this for example:

TableName services = > ProjectName Service
TableName customers = > ProjectName Customer
TableName service_customer = > ProjectName CustomerService

Fields of "client_service" (They go in singular because they make reference to the names of the models)
  - servicio_id (singular)
  - client_id (singular)

// Modelo Servicio
class Servicio extends Model
{
  public function clientes() {
      return $this->belongsToMany(Cliente::class);
  }
}

// Modelo Cliente
class Cliente extends Model
{
  public function servicios() {
      return $this->belongsToMany(Servicio::class);
  }
}

This should work, I hope I have been helpful.
Greetings

    
answered by 26.03.2018 в 07:47