简体   繁体   中英

WhereNotIn In Laravel 4

I have issue with whereNotIn in laravel 4 , i need to get that not in from array of ids as bellow

 $studAttend  = StudentAttendees::select('subject_schedule_id')->distinct()->get();

$Check  = SubjectSchedule::whereNotIn('id', array($studAttend))->get();

Also I tried to change my code to loop over $studAttend as bellow

$studAttendArray = array();

foreach ($studAttend as $stdAtt)
{
    $studAttendArray [] = $stdAtt ; 
}

To become

$Check  = SubjectSchedule::whereNotIn('id', $studAttendArray)->get();

The issue that the result of $Check variable return null [] but when i use the following manual code the result if $Check return the row that i need it

$Check  = SubjectSchedule::whereNotIn('id', array(1,2,3,4,5,6,7 ))->get();

Note the above numbers inside the array they is the same result of $studAttend OR $stuAttendArray .

Any Suggestions ?

I Solved It just by using toArray() as bellow

$Check  = SubjectSchedule::whereNotIn('id', $studAttend->toArray());

Now not return []

The functionality looks fine, you just need to make sure you're passing in the correct array.

This code should work a little easier:

// 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();

你可以在这样的地方使用

$Check  = SubjectSchedule::whereIn('id', '!=', $studAttendArray)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM