我在laravel 4中遇到了whereNotIn
的问题,我需要将其从ids的数组中删除,如下所示
$studAttend = StudentAttendees::select('subject_schedule_id')->distinct()->get();
$Check = SubjectSchedule::whereNotIn('id', array($studAttend))->get();
此外,我尝试将代码更改为$studAttend
,如下所示
$studAttendArray = array();
foreach ($studAttend as $stdAtt)
{
$studAttendArray [] = $stdAtt ;
}
成为
$Check = SubjectSchedule::whereNotIn('id', $studAttendArray)->get();
$Check
变量的结果返回null []
的问题,但是当我使用以下手动代码时$Check
返回我需要它的行的结果
$Check = SubjectSchedule::whereNotIn('id', array(1,2,3,4,5,6,7 ))->get();
注意数组中的上述数字与$studAttend
或$stuAttendArray
的结果相同。
任何建议?
答案 0 :(得分:1)
I Solved It just by using toArray()
as bellow
$Check = SubjectSchedule::whereNotIn('id', $studAttend->toArray());
Now not return []
答案 1 :(得分:1)
功能看起来很好,你只需要确保传入正确的数组。
此代码应该更容易一些:
// lists will return an actual array with just the field specified as the values.
// array_unique will make sure the values are unique.
$studAttend = array_unique(StudentAttendees::lists('subject_schedule_id'));
// pass the array to your next query
$Check = SubjectSchedule::whereNotIn('id', $studAttend)->get();
答案 2 :(得分:0)
你可以使用不喜欢的地方
$Check = SubjectSchedule::whereIn('id', '!=', $studAttendArray)->get();