php - Extracting value of selected attribute from Laravel Eloquent ORM MySql query result
I have a variable that saves result of Elqouent ORM MySql query result as such:
$from = DB::table('stops')
->select('id',)
->where('name','=',$data['from'])
->get();
So I wish to know what number is the ID attribute. Result of query ($from variable contains) is following:
[{"id":7}]
Now how do I "extract" that number 7 into a variable? i.e $id = .... ?!? Im not sure what kind of object or array or what that result is. Square brackets indicate array, curly maybe object ? I tried several ways to "get my hands on" that number but I was unsuccessful.
Answer
Solution:
just use 'first()->id'
$value = DB::table('stops')
->select('id',)
->where('name','=',$data['from'])
->first();
$result=null;
if($value!=null) $result=$value->id;
return $result;
Source