Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

Query Correction

I am using this Query.

Safety::with( ['imageName'] )->where( ['property_id', '=', $property->property_id ], ['unit_id', '=', $property->unit_id ],['type','=', 'gas' ])->latest()->first();

I am getting error SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' .

Is there any issue in the Query ?

0 likes
4 replies
gych's avatar

You are adding multiple arrays to the where clause.

You can solve it like this, all the separate arrays put inside one single array in the where clause

Safety::with( ['imageName'] )->where([ 
				['property_id', '=', $property->property_id ], 
				['unit_id', '=', $property->unit_id ],
				['type','=', 'gas' ]
])->latest()->first()

Or use separate where clauses

Safety::with(['imageName'])
    ->where('property_id', $property->property_id)
    ->where('unit_id', $property->unit_id)
    ->where('type', 'gas')
    ->latest()
    ->first();

There are other solutions that you can use like using a RAW query but the syntax for this depends on which DB you use and personally I would do it like in the examples I gave you.

1 like
Nuxtor's avatar

The error you're encountering, "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause'," typically indicates an issue with how the where clause is being structured in your query. In Laravel, when using* where* with an array of conditions, each condition needs to be an array itself.

In your query, it seems like you're trying to pass multiple conditions to the where method, but the way they are structured is causing the issue. You're passing a single array with nested arrays, but each condition array should be directly passed to the where method.

Here's how you can correct it:

Safety::with('imageName')
    ->where('property_id', '=', $property->property_id)
    ->where('unit_id', '=', $property->unit_id)
    ->where('type', '=', 'gas')
    ->latest()
    ->first();

Each where condition is passed as a separate method call. This should resolve the SQL error you're encountering.

amitsolanki24_'s avatar

@afoysal You can use this Safety::with( ['imageName'] )->where( [ 'property_id', => $property->property_id, 'unit_id' => $property->unit_id, 'type' => 'gas' ])->latest()->first();

Or you can also use this query with separate where()

Safety::with( ['imageName'] ) ->where('property_id', '=', $property->property_id) ->where('unit_id', '=', $property->unit_id) ->where('type','=', 'gas' ) ->latest() ->first();

Please or to participate in this conversation.