Myat Zuckerberg
Myat Zuckerberg

Reputation: 85

Get specific value from array in laravel

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

Answers (3)

Astergo
Astergo

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

Death-is-the-real-truth
Death-is-the-real-truth

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

M Mamoon Khan
M Mamoon Khan

Reputation: 179

you can use pluck() function to get the first record of an array

Upvotes: 1

Related Questions