Doubt in the relations of the Eloquent - Laravel 5:2

Asked

Viewed 30 times

1

Good afternoon sorry if I missed any similar question, I searched extensively both here and in English and found nothing like it,

I have the following Model in the database

contract
   id
   due_date

contract_service
   id
   price
   contract_id
   type_id

service
   id 
   name
   desc
   base_price

The models are Contract, Contractservice, Service, follow the 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 Dallas I search for the name of the contract services this way (by being hasMany)

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

Is there any way we’ve narrowed down that extended relationship and do something like this below?

$contract->service[0]->name

The service table is where I keep the type of Services to be provided, it would be something like conctract_service_description (which Opencart uses using inner_join if I’m not mistaken).

  • What are relationships? Describe in questions...

  • That’s right, Cassiano. If you’re going to join, you don’t need relationships in Model.

  • If you make a join in the search will be as you are requesting.

  • I put the models

  • I don’t think you need one JOIN no. You can use hasManyThrought.

  • The hasManyThrough does not work for this case, should be something like hasManyThroughBelongsTo. I found a discussion that solved this problem, follow the link: http://laravel.io/forum/03-04-2014-hasmanythrough-with-many-to-many Vlw personal!

Show 1 more comment

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.