You can use the pluck() method to retrieve a single value instead of the entire row. For example:
$addressId = DB::select(/* query */)->pluck('id'); //might be `roundsDetails.id`, I'm not positive
$complaint->complaintAddressId = $addressId;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
Sorry complete re-write including title of this discussion as I figured out a method of solving original error, now just looking for best practice.
I've got the following code that gives me the id of a street address
$address = DB::select('SELECT roundsDetails.id FROM roundsDetails
INNER JOIN streets ON roundsDetails.streetsId = streets.id
WHERE streets.id = 307 AND roundsDetails.streetNumber = 21');
if I dump the output I get this
array (size=1)
0 =>
object(stdClass)[259]
public 'id' => int 8659
I want to use the 8659 to be persisted in a database, but not sure if this way is the best
$complaint->complaintAddressId = $address[0]->id;
Any other ways you would tackle this?
Ah, DB::select() executes a select query without using the query builder. In that case the simplest thing would be this:
$address = DB::selectOne(/* query */);
$complaint->complaintAddressId = $address->id;
Please or to participate in this conversation.