我正在创建一个组织的群组制作的编辑页面。每个小组由一名导师和一些受指导者组成。
问题是我无法使用结果进行迭代,因为它是集合和用户的集合,而不是给(直接)用户集合。 错误是这个
ErrorException in dd3280396f5fc9a234b8faac1b17b55bd5c30ae2.php line 77:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: E:\Documents\Karya\simentor\resources\views\group\edit.blade.php)
以下是dd($mentees)
Collection {#260 ▼
#items: array:2 [▼
0 => Collection {#286 ▼
#items: array:1 [▼
0 => User {#284 ▶}
]
}
1 => User {#261 ▶}
]
}
为了让所有没有任何小组的导师和受指导者得到被编辑小组的导师和受指导者,我在GroupController.php
public function edit($id)
{
$group = Group::find($id);
//get mentor and mentee who don't have group yet
$unassignedMentors = User::whereHas(
'roles', function ($q) {
$q->where('name', 'mentor');
})
->doesntHave('groups')
->get();
$unassignedMentees = User::whereHas(
'roles', function ($q) {
$q->where('name', 'mentee');
})
->doesntHave('groups')
->get();
//get mentor and mentee of the to-be-edited group
$assignedMentor = $group->mentor();
$assignedMentee = $group->mentee();
//combine those mentors and mentees into a collection
$mentors = $unassignedMentors->prepend($assignedMentor);
$mentees = $unassignedMentees->prepend($assignedMentee);
return view('group.edit', compact('group', 'mentees', 'mentors', 'assignedMentor', 'assignedMentee'));
}
mentor()
中定义的mentee()
和Group.php
方法
public function mentor()
{
return $this->users()->whereHas(
'roles', function($q){
$q->where('name', 'mentor');
})
->first();
}
public function mentee()
{
return $this->users()->whereHas(
'roles', function($q){
$q->where('name', 'mentee');
})
->get();
}
以下是在edit.blade.php
@foreach ($mentees as $mentee)
<tr>
<td><input type="checkbox" name="mentee_id[]" value="{{$mentee->id}}" {{ array_has($assignedMentee, $mentee) ? 'checked' : '' }}></td> --> line 77
<td>{{ucwords($mentee->profile->name)}}</td>
</tr>
@endforeach