@muehleis Confirm that there are records in your Device table that meet both conditions specified in the where clause (user_id and uuid). Executing the query without the first() method and checking if it returns any results
Apr 15, 2024
4
Level 2
first() ignores the where clause
Hi,
when i execute
$code = Device::where('user_id', auth()->id())->where('uuid', $this->codeId)->first()->value('connect_token');
i always get the first record of my mysql-Database, so he ignores the uuid. When i remove first() it works:
$code = Device::where('user_id', auth()->id())->where('uuid', $this->codeId)->value('connect_token');
Can anyone tell me what i'm doing wrong?
Level 122
The mistake is that value() runs a query.
So, first() runs a query and returns a model.
You then do $model->value() which runs a second query using the model as the basis for the query.
Your solution is to use one method or the other, not both.
$code = Device::where('user_id', auth()->id())
->where('uuid', $this->codeId)
->value('connect_token');
or
$code = Device::where('user_id', auth()->id())
->where('uuid', $this->codeId)
->first()
->connect_token;
2 likes
Please or to participate in this conversation.