I want to select from user table name and address columns where name is John. This query return null. I know in database is user which has name John. I would like to know if this query is property ?
$query = User::where('name', '=', 'John')->select(['name', 'address']);
4
Answers
You were not running your query, you need
get()
which returns a collection orfirst()
if you want to retrieve a single record.For your case i think
first()
fits more.Or following your approach you don’t need select in the end but instead:
You can simply use this
Just call
get()
at the end:Notice: Maybe you’re inaccurate in case sensitive. I mean the user name might be "john" not "John"
You could use
first()
, which does the following:In the code, you will find:
Note that
first()
takes parameter which columns you want to select. So in your case, you can use: