So I have the following where clause where I am searching the name "Gold" inside of my tier table.
$tier = Tier::where(['name' => 'Gold'])->get(['id']);
dd($tier);
When I do the above the "id" is listed inside of the attributes but when I do as below it gives me
Undefined property: Illuminate\Database\Eloquent\Collection::$id
dd($tier->id);
All I want is to get the Id of the record where the name is "Gold".
To get actual model you should use first()
instead of get()
. Because there are potentially multiple rows that match get()
will return a collection.
$tier = Tier::where(['name' => 'Gold'])->first(['id']);
dd($tier->id);
If you really need only the id there's even a simpler function to that, pluck()
.
It will get one attribute from the first result:
$id = Tier::where(['name' => 'Gold'])->pluck('id');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments