Multiple where clause, Laravel

Beusebiu

I try to do a query with 2 where clauses, but I get a bad response, not sure why.

$history = AnswerHistory::where('question_id', '=', $id)
     ->where(function ($query) {
            $query->where('answer_type', '!=', "skipped");
      })
      ->get();

For a specific $id in database I have 5 rows, 4 with answer_type = 'skipped, but the 5-one is NULL.

My response is null with this code, if I remove second where I get 5 items that includes "skipped" answers.

Wanted response is 1 row, where answer_type != 'skipped'.

PS. I also tried the answer from here.

enter image description here

Julian S

The Problem is, a varchar that is null is null and not a varchar with a value. So it can not be checked with = or != 'string'. So you have to check if answer_type is != skipped or null

$history = AnswerHistory::where('question_id', '=', $id)
    ->where(function ($query) {
        $query->where('answer_type', '!=', "skipped")
            ->orWhereNull('answer_type');
    })
    ->get();

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Multiple queries in WHERE clause

分類Dev

Multiple Case conditions in where clause

分類Dev

Laravel using where clause on a withCount method

分類Dev

Apply OR clause in WHERE or HAVING in eloquent laravel

分類Dev

Laravel Equals Returning Empty In Where Clause

分類Dev

PDO select multiple table with where clause

分類Dev

Multiple Where clause in Android SQL statement

分類Dev

SQL Multiple Joins on same table with where clause

分類Dev

MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

分類Dev

MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

分類Dev

Multiple rows in single where ... like clause

分類Dev

IF in WHERE clause

分類Dev

InfluxDB - Including multiple values in where clause based on tags

分類Dev

Join Statement using Query builder with multiple where clause

分類Dev

Multiple Linked Servers in one select statement with one where clause, possible?

分類Dev

Laravel: How to use multiple where with Request

分類Dev

SAS Same column selection multiple times using multiple where clause as different output columns

分類Dev

Dynamic where clause in lambda

分類Dev

MINUS vs NOT in where clause

分類Dev

SQL Ignoring WHERE clause

分類Dev

Where clause with calculated column

分類Dev

Using CASE in WHERE clause?

分類Dev

Conditional WHERE clause using IN

分類Dev

expdp where clause errors

分類Dev

Using a %ROWTYPE in a WHERE IN clause

分類Dev

Where clause with a conditional condition

分類Dev

Nested decode in where clause

分類Dev

Query error in WHERE clause

分類Dev

where clause not working in mysql