我的User
型有很多Target
,反之亦然.
现在,我已经得到了给定的User
和给定的Target
,并且我想从它们的关系中访问透视数据.数据透视表列名为type
How can I achieve this?
我的User
型有很多Target
,反之亦然.
现在,我已经得到了给定的User
和给定的Target
,并且我想从它们的关系中访问透视数据.数据透视表列名为type
How can I achieve this?
On the relationships for both User
and Target
, tack on a ->withPivot('type')
which will instruct Laravel to include that column. Then once you have your result set, you can access the field with $user->pivot->type
.
如果您没有迭代一个集合,但是有一个用户和他们的一个目标,并且想要type
字段,那么可以使用$target = $user->targets->find($targetId)
并使用$target->pivot->type
访问该类型.
http://laravel.com/docs/4.2/eloquent#working-with-pivot-tables点多