Doubt in Eloquent Relationships - Laravel 5.2

1

Good afternoon, sorry if I had any similar questions, I searched extensively both here and in English and found nothing similar,

I have the following Template in the database

contract
   id
   due_date

contract_service
   id
   price
   contract_id
   type_id

service
   id 
   name
   desc
   base_price

The templates are Contract, ContractService, Service, follow their respective relationships:

class Contract extends Model
{
    public function service() {
        return $this->hasMany('App\ContractService');
    }
}

class ContractService extends Model
{    
    public function type() {
        return $this->belongsTo('App\Service', 'type_id');
    }
}

class Service extends Model
{    

}

In laravel I look for the name of the contract services in this way (for being hasMany)

$contract->service[0]->type->name

Is there any way to reduce this extensive relationship and do something like this?

$contract->service[0]->name

The service table is where I save the type of Services to be rendered, it would be something like conctract_service_description (which OpenCart uses using inner_join if I'm not mistaken).

    
asked by anonymous 24.11.2016 / 18:06

1 answer

0

I found the solution in the Laravel forum itself, but it is not part of the framework core.

The answer is already anchored by the link. In it is the explanation of how to implement the new HasManyThroughBelongsTo relation that answers my question: link

    
24.11.2016 / 18:55