Laravel Eloquent pick only one field of a record

Asked

Viewed 4,863 times

1

I’m new to the Laravel and I have a little problem

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

from this consultation, as I take the field id?

I tried to

$profile_id->id

but it made a mistake

Undefined Property: Illuminate Database Eloquent Collection::$id

2 answers

3


I’ll have to answer because other people have already fallen for that mistake.

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

When the Collection, means that you have brought more than one dice.

When does the model, means that you have brought one die only.

When you use the method get at the end of the query, it is bringing several results. When you use first or find, is bringing only one.

1

I got!!

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.