반응형
Eloquent-같지 않은 곳
현재 최신 Laravel 버전을 사용하고 있습니다.
다음 쿼리를 시도했습니다.
Code::where('to_be_used_by_user_id', '<>' , 2)->get()
Code::whereNotIn('to_be_used_by_user_id', [2])->get()
Code::where('to_be_used_by_user_id', 'NOT IN', 2)->get()
이상적으로는를 제외한 모든 레코드를 반환해야 user_id = 2
하지만 빈 배열을 반환합니다. 이 문제를 어떻게 해결합니까?
Code::all()
4 개의 레코드가 모두 반환됩니다.
코드 모델 :
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Code extends Model
{
protected $fillable = ['value', 'registration_id', 'generated_for_user_id', 'to_be_used_by_user_id', 'code_type_id', 'is_used'];
public function code_type()
{
return $this->belongsTo('App\CodeType');
}
}
사용 where
와 !=
함께 연산자whereNull
Code::where('to_be_used_by_user_id', '!=' , 2)->orWhereNull('to_be_used_by_user_id')->get()
들어 where field not empty
이 나를 위해 일한 :
->where('table_name.field_name', '<>', '')
이것이 작동하는 것 같지만
Code::where('to_be_used_by_user_id', '!=' , 2)->orWhereNull('to_be_used_by_user_id')->get();
일반적으로 where 절에서 "or"는 인덱스를 사용하는 쿼리를 중지하므로 큰 테이블에는 사용하지 않아야합니다. "키 조회"에서 "전체 테이블 스캔"으로 이동합니다.
대신 Union을 사용해보십시오
$first = Code::whereNull('to_be_used_by_user_id');
$code = Code::where('to_be_used_by_user_id', '!=' , 2)
->union($first)
->get();
참고 URL : https://stackoverflow.com/questions/28256933/eloquent-where-not-equal-to
반응형
'program tip' 카테고리의 다른 글
Postgres JSON 배열에 문자열이 포함되어 있는지 확인 (0) | 2020.10.07 |
---|---|
Gmail에 2 단계 인증이 설정되어 있으면 이메일 전송이 실패합니다. (0) | 2020.10.07 |
Python 스크립트 내에서 UAC 상승을 요청 하시겠습니까? (0) | 2020.10.07 |
Delphi XML 데이터 바인딩 마법사를 사용할 때 필요한 태그가 없습니다. (0) | 2020.10.06 |
Robolectric으로 사용자 지정보기 테스트 (0) | 2020.10.06 |