Get Attribute from Elequent where clause

Steve

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".

lukasgeiter

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.

edited at
0

Comments

0 comments
Login to comment

Related

SQL SERVER: WHERE clause in subquery to get attribute from outer query

How to use alias in where clause laravel elequent with postgres

Get highest value from column in where clause

Get the highest id from a specific where clause

Get condition of WHERE clause from table

Get value from Database to Linq where clause

Get the sum() in a WHERE clause

Where clause from a subquery

get all rows from db if parameter added to where clause is null

Join multiple entities using where clause and get result from that

How to get a value from one pyspark dataframe using where clause

how to get Multiple tuples from where Clause in sqlite database

How to get the where clause from IQueryable defined as interface

Get lambda expression from where clause or IQueryable/IEnumerable

How to get variables from MSQL query with WHERE IN clause

Get max/min value of the column independet from where clause

Parse where clause query from Rest Get call

Laravel: Add Where Clause To Appended Custom Attribute

powershell xml select attribute value where clause

refer to array from SELECT clause in WHERE clause

MDX where clause vs From clause

Get Substring and Use it In Where Clause

Where clause completed from a column

Where clause from array column

MySQL select from where clause on

SQL INSERT from where clause to another where clause

Why do I get a conversion failed error when I use my attribute on my WHERE clause but it works when I don't?

Get rows from dataframe using index array from numpy where clause

How to get values from MySQL database with PHP code using WHERE clause and variable set from url