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

movepixels's avatar

Query always returns a value

I am running this query:

        ['id', '=', $value],
        ['attribute_question_id', '=', $question->id]])->select(['id', 'attribute_question_id'])->firstOrFail();

and it always returns a value when it should not. If I have $value = 0 I know there is no record in the database with id = 0 so it should fail, but it simply moves along and pulls the record where 'attribute_question_id', '=', $question->id

This is more of a validation use query so I am checking that yes there is a record in the database where id = $value AND attribute_question_id = $question->id

Not an either or like it appears to be doing.

Thoughts, suggestions?

Thanks, Dave

0 likes
8 replies
Snapey's avatar

it would help if we could see the whole query

movepixels's avatar

Sorry, my bad

AttributeAnswer::where('id', '=', $value)->where('attribute_question_id', '=', $question->id)->select(['id', 'attribute_question_id'])->get(); //first // firstOrFail

It always returns some record no matter what. I am only concerned with the $value since its user submitted, but i need to match with a question being asked so that var is set in the backend. I am currently entering '''$value = 0''' or anything I know is not valid but the query still returns a record.

I even tried using count(); instead of get() find() and it still returns a record.

Logs:

INFO: value = 0 and question id = 1c3caede-5bc1-11e7-8e79-40167eb29fbd INFO: record = 1

INFO: value = cf01af7c-5c0e-11e7-b0e0-40167eb29fbd and question id = 939ef982-5bc1-11e7-b285-40167eb29fbd INFO: record = 13

As you can see when value = 0 and the question_id is there it still says yes there is a matching record

movepixels's avatar

Even manually hardcoding the $value still pulls the records

$count = DB::table('attribute_answers')->where('id', '=', 0)->where('attribute_question_id', '=', $question->id)->count();
      \Log::info('count = ' . $count);

Logs: count = 1 count = 5 count = 12 count = 7 count = 12 count = 1 count = 12

But since there is no id = 0 every count should be 0 the count showing is the number of records with 'attribute_question_id', '=', $question->id skipping the id params all together so it appears.

Snapey's avatar

and if you leave that part out of the query altogether?

bradbforbes's avatar

When you're working with queries and things aren't making sense, sometimes it's best to start with the simplest thing then add to it until it's what you need.

dd(AttributeAnswer::all());

dd(AttributeAnswer::where('id', $value)->get());

dd(AttributeAnswer::where('id', $value)->where('attribute_question_id',  $question->id)->get());

// ...

Snapey's avatar

just checking what column type you have for the uuid and that you have set incrementing=false

movepixels's avatar

Yes it is set to false for all models incrementing=false

Snapey's avatar

I have seen this before and I'm racking my brains. Unfortunately Laracasts does not allow you to search just threads you participated in.

Its something of to with the way uuid is being used as a string but the value passed to the where is an integer. Dump the SQL and you will probably find that the uuid is not quoted.

eg

     select * from `attribute_answers` where `attribute_question_id` = 1c3caede-5bc1-11e7-8e79-40167eb29fbd

instead of

     select * from `attribute_answers` where `attribute_question_id` = '1c3caede-5bc1-11e7-8e79-40167eb29fbd'

Simplify your code and just use the where's individually first. I have a feeling its nothing to do with the $value

Edit:

Actually, if both primary keys are string columns and you say where id=0 then mysql tries to compare the string column to 0 as an integer

Try this;

$count = DB::table('attribute_answers')->where('id', '=', (string) 0)->where('attribute_question_id', '=', (string) $question->id)->count();
      \Log::info('count = ' . $count);

Please or to participate in this conversation.