Related Eloquent query

1

Hello, I'm trying to get the data of a 'client'

I have 3 Tables

|Cliente|Cliente_servicio|Servicios

In the client table, I have a client's data

In the Customer_Service table I have the IDcliente, IDservicio, Description

in the Service table only the ID and name of the service

My relationship I have it So of the model Client is like this

 public function clienteservicio()
 {
 	return $this->HasMany(Clienteservicio::class);
 }

From the Customer_Service model

	public function cliente()
	{
		return $this->belongsTo(Cliente::class,'cliente_id');
	}


	public function servicio()
	{

		return $this->belongsTo(Servicio::class,'servicio_id');
	}

And finally Service

 public function Clienteservicio()
	{
		return $this->belongsTo(Clienteservicio::class);
	}
So far to see the data of a client shows me this way, But where does CustomerService say how could he get the name to the service he belongs to?

=> App\Cliente {#2925
     id: 2,
     nick: "test",
     nombre: "kjsdjkfsjkd",
     apellidoP: "jksjdjfksjkd",
     apellidoM: "jksjdkfjsdjkf",
     vive: "sdfjksdjkfjksd",
     created_at: "2018-10-07 22:08:15",
     updated_at: "2018-10-07 22:08:15",
     Clienteservicio: Illuminate\Database\Eloquent\Collection {#2932
       all: [
         App\Clienteservicio {#2937
           id: 1,
           cliente_id: 2,
           servicio_id: 1,
           descripcion: "excelente",
           created_at: null,
           updated_at: null,
         },
         App\Clienteservicio {#2935
           id: 2,
           cliente_id: 2,
           servicio_id: 2,
           descripcion: "ok",
           created_at: null,
           updated_at: null,
         },
       ],
     },
   }

so far this is how I do my query, I was testing with tinker. but I can not get that data more than I'm interested in.

$prueba = Cliente::With(['Clienteservicio'])
        ->where('nick','test')
        ->first();

my relationships may be wrong but I can not understand.

    
asked by Alex Burke Cooper 08.10.2018 в 08:21
source

2 answers

0

Try this, assuming you are in laravel 5. *

Cliente.php
 public function service()
 {
    return $this->HasMany(App\Service);
 }
Service.php
public function cliente() {
        return $this->belongsTo('App\Cliente');
}

$services= Cliente::find($id)->service()->get();
$services[0]['descripcion'] // utiliza un foreach o un for

customers who are in a service

 $clients = Servicio::find($id)->cliente()->get();
 $servicesInclient[0]['nombre']

If you do not have the id:

 $clients = Servicio::where('atributo','valor')->first()->cliente();
    
answered by 08.10.2018 в 16:38
0

As I do not see the structure of your tables, I will give you an example that I did myself and it is as follows:

  

Group.php

public function group_days()
{
    return $this->hasMany(\App\Models\GroupDay::class, 'idGroup');
}
  

GroupDay.php

public function day()
{
    return $this->belongsTo(\App\Models\Day::class, 'idDay');
}
  

Day.php

public function group_days()
{
    return $this->hasMany(\App\Models\GroupDay::class, 'idDay');
}

And we build our query with Eloquent using ->with() in order to recover the associated models for this case use the following Eloquent query:

$oGroup = AdmAcaGroup::where('id', '=', $id)->where('idStatus', '=', CStatus::$ACTIVO)
    ->with(array('adm_aca_group_days' => function ($query) {
        $query->select('adm_aca_group_day.idGroup', 'adm_c_day.name')
            ->join('adm_c_day', 'adm_aca_group_day.idDay', '=', 'adm_c_day.id');
    }));

dd($oGroup->first()->toJson());

I hope and serve you.

    
answered by 08.10.2018 в 18:38