0
votes

I have a problem with laravel orm. I have three tables table A(id,.......), Table B(id, A_id ,C_id,.....), table C(id,....). and table A and B has one to many relation and table B and C have one to one relation. How can I use hasMany on C from table A. Like

class aModel {
function b(){
return hasMany(app\Models\bModel');
}
// i want to do this
function c(){
return hasMany('App\Models\cModel');
}
}
class bModel{
function a(){
return belongsTo('App\Models\aModel);
}
function c(){
return belngsTo('App\Models\cModel');
}
}
like aModel::with(['b'])->get(); i want.
$a = aModel::with( ['c] )->get();

anyone please................

3

3 Answers

0
votes

Take a look at the hasManyThrough() method.

return hasManyThrough('App\Models\cModel', 'App\Models\bModel');
0
votes

oh actually its little tricky.. i found my solution..
solutin is
aModel::where(['b.c'])->get();

0
votes

You can use hasManyThrough():

public function cModels()
{
    return $this->hasManyThrough('App\Models\cModel', 'App\Models\bModel');
}

Or you can just use nested eager loading:

aModel::with('bModels.cModels')->get();