Laravel pivot: Get model from withPivot() -
i'm building application using laravel 4 have stumbled across problem pivot tables.
i've got user model, establishment model, & studylevel model.
for moment find establishment user has been @ use following code in user model:
public function establishments() { return $this->belongstomany('establishment')->withpivot('start', 'stop', 'study_level_id')->withtimestamps(); }
the establishment_user (pivot) table has following columns:
id | establishment_id | user_id | start | stop | study_level_id
to list of establishments user use following in controller:
$establishments = $user_repo->find($user_id) ->with(['establishments', 'establishments.usersubjects' => function ($query) use ($user) { $query->where('user_id', $user->id); }]) ->get();
my problem response gives me id of studylevel have information studylevel model. possible information studylevel table using withpivot() ?
thanks in advance, appreciated.
the ideal way have model pivot table. there alternatives whereby can have relationships defined in models, using field pivot_study_level_id, wouldn't work in every situation , more trouble it's worth.
just setup model establishment\user
, let handle relationships.
Comments
Post a Comment