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

bassemshoukry's avatar

Laravel DB query turning array instead of object

I have this query in laravel am trying to use but it's returning an array is there a method to have it an object instead?


$x = DB::table('user_profiles')
 ->where('id',$holidays[0]
->profile_id)
->select('first_name')->get();

0 likes
5 replies
munazzil's avatar

You have to take the first collection only using first

       $x = DB::table('user_profiles')
      ->where('id',$holidays[0]
      ->profile_id)
     ->select('first_name')->first();
Nakov's avatar
Nakov
Best Answer
Level 73

Why don't you use Eloquent for that then?

If you have a UserProfile model, use this:

$profile = UserProfile::findOrFail($holidays[0]->profile_id);

$profile->first_name; // will give you the name

1 like
ketansavaliya's avatar

It actually returns an array of objects. To make an individual object to be an array just cast it. For example: (array)$data[0]

Please or to participate in this conversation.