Laravel Eloquent pick up only one field from a record

1

I'm new to laravel and I have a problem

$profile_id = Profile::where("customer_id",$data["customer_id"])->where('is_default', 1)->select('id')->first()->get();

From this query, how do I get the field id ?

I tried

$profile_id->id

but gave an error

  

Undefined property: Illuminate \ Database \ Eloquent \ Collection :: $ id

    
asked by anonymous 04.02.2016 / 16:53

2 answers

2

I will have to respond because other people have already fallen into this error.

There are two classes that Laravel can return when you query: Collection or model.

When you return Collection , it means that you have brought more than one data.

When you return model , it means that you have only one data.

When you use the get method at the end of the query, you are bringing multiple results. When you use first or find , you are only bringing one.

    
04.02.2016 / 17:47
1

I got it !!

Profile::where("customer_id",$data["customer_id"])->where('is_default', 1)->select('id')->first()->id;
    
04.02.2016 / 17:01