Reputation: 85
I'm using Laravel. Here is my array -
[{"emp_id":1,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"},{"emp_id":2,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"},{"emp_id":3,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"},{"emp_id":4,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"},{"emp_id":6,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"},{"emp_id":7,"shift":"first","status":"Present","is_unpaid":0,"start":"2018-03-21 08:00:00","end":"2018-03-21 12:00:00"}]
And this is my array output code -
if(empty($employee->emp_record) ){
$sheet = array();
}else{
$sheet = unserialize($employee->emp_record);
}
return $sheet;
I want to get value of emp_id and show in the view. When I use return $sheet->emp_id;
Trying to get property of non-object This error happened.
Upvotes: 1
Views: 8581
Reputation: 1
If you want all, then apply a loop like below:-
$sheet = [];
if(!empty($employee->emp_record) ){
foreach($sheet as $shet){
$sheet[] = [
"emp_id" => $shet->emp_id
//.....rest data set
];
}
}
return $sheet;
Upvotes: 0
Reputation: 72289
To get the first record only, you need to use:-
return $sheet[0]->emp_id;
But if you want all, then apply a loop like below:-
foreach($sheet as $shet){
echo $shet->emp_id;
}
Output:- https://eval.in/995586
Upvotes: 1
Reputation: 179
you can use pluck()
function to get the first record of an array
Upvotes: 1