背景 我正在做的是我创建了一个页面,其中显示了一个包含参加者和活动复选框的表格。它一直工作,直到我试图通过一个数据透视表通过属于多个关系的查询来检查我的复选框。
我已经查看了其他一些线程,并尝试使用相同的方法检查现有条目数据的数据透视表。我正在使用contains()函数来查看我的与会者和scheduled_program_segment是否匹配,但由于某种原因,我的代码出现此错误。
问题:
我收到错误Call to member function contains() on a non-object
。
以下是我认为相关的一些代码
@foreach( Auth::user()->attendee as $attendee )
<td class="col-lg-1">
{{
Form::checkbox(
($program->name . $attendee->first_name),
'1',
$attendee->scheduled_program_segments->contains($attendee->id)
)
}}
我也尝试过改为$与会者。同样的错误 另外,当我跑
时 {{$attendee->scheduled_program_segments()->get()}}
我得到以下我假设的意思是我正确设置了数据透视表和模型,但它没有工作,所以也许我错了。
[{"id":"1","created_at":"2014-11-14 15:19:44","updated_at":"2014-11-14 15:19:44","name":"Minecraft","description":"","date":"2014-11-06","cost":"20.00","start_time":"16:30:00","end_time":"17:30:00","location":"Cookstown","scheduled_program_id":"1","program_segment_id":"1","pivot":{"attendee_id":"1","scheduled_program_segment_id":"1"}},{"id":"1","created_at":"2014-11-14 15:19:44","updated_at":"2014-11-14 15:19:44","name":"Minecraft","description":"","date":"2014-11-06","cost":"20.00","start_time":"16:30:00","end_time":"17:30:00","location":"Cookstown","scheduled_program_id":"1","program_segment_id":"1","pivot":{"attendee_id":"1","scheduled_program_segment_id":"1"}}]
答案 0 :(得分:1)
$attendee->scheduled_program_segments
是mm关系,所以很明显它返回null
,因为Laravel 4+关系需要为camelCased
才能作为dynamic properties(是你正在尝试做的事情。)
因此,您可以将关系重命名为scheduledProgramSegments()
,然后将其命名为:
$attendee->scheduledProgramSegments
// or
$attendee->scheduled_program_segments // yes, this will work as well
或更改该行如下(我不建议):
$attendee->scheduled_program_segments()->get()->contains(...)