Reputation: 60
I'm doing an Eloquent query in my Profession model, i ran an where query, retrieve the first value, then caught only the value in "id" column with ->value('id')
//This always return 1
$profession = Profession::where('name', $profession)->first()->value('id');
//This return the right value
$profession = Profession::where('name', $profession)->first()->id;
Does value() function depends on some other aspect of the query/configuration? Couldn't find anything in Laravel query builder documentation.
Upvotes: 0
Views: 1258
Reputation: 111
There is a helper function in laravel with the name value . It will always returns that value it is given for example value(true) is true But for eloquent if you want to access the fields either you use pluck function like ->pluck(id) it will give you ids only
Upvotes: 0
Reputation: 29079
Profession::where('name', $profession)->first()
returns a model. You cannot call value
on a model. However, you may call it on a query builder as explained in the docs?
So instead of calling
$profession_id = Profession::where('name', $profession)->first()->id;
you could achive the same thing with
$profession_id = Profession::where('name', $profession)->value('id');
Upvotes: 2