S
S
sandrain2015-08-05 15:56:55
Laravel
sandrain, 2015-08-05 15:56:55

How to create a relation in Laravel 5 through another relation?

Essence of the question. There are models and connections.

class Garage extends ApiModel
{
    public function car()
    {
        return $this->belongsTo('Api\Models\Cars\Type', 'carId', 'car_id')->where('lang','pkw_de');
    }
}

class Type extends Model
{

    public function model()
    {
        return $this->belongsTo('Api\Models\Cars\Models', 'model_id', 'model_id')->where('lang', \Config::get('parameters.project.project_alias', 'pkw_de'));
    }

    public function maker()
    {
        return $this->belongsTo('Api\Models\Cars\Manufacturer', 'maker_id', 'maker_id')->where('lang', \Config::get('parameters.project.project_alias', 'pkw_de'));
    }
}

class Models extends Model
{
    public function group()
    {
        return $this->belongsTo('Api\Models\Cars\Group', 'group_id', 'group_id')->where('lang', \Config::get('parameters.project.project_alias', 'pkw_de'));
    }
}

I get the data .... everything is ok
$cars = GarageModel::with('car','car.model','car.maker', 'car.model.group')->where('customerId', $customerId)->orderBy('createdAt', 'desc')->get();

Only in this form the data does not fit me a little. Is it possible somehow to display the group at the same level as the model and the maker: 'car.model', 'car.maker', 'car.group'.
Who knows how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-08-05
@sandrain

The question is not clear.
But I'll try to guess that you need something like laravel.com/docs/5.1/eloquent-relationships#has-ma...
Only you have to change the table structure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question