現在、最新の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"はインデックスを使用するクエリを停止します。 「キー検索」から「フルテーブルスキャン」へ
代わりに nion を試してください
$first = Code::whereNull('to_be_used_by_user_id');
$code = Code::where('to_be_used_by_user_id', '!=' , 2)
->union($first)
->get();