1

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.

2
  • You have multiple records and each one contains an emp_id so you need to choose which one to return or access like this return $sheet[0]->emp_id; Commented Apr 26, 2018 at 19:10
  • what's the output of:- var_export($sheet);? Commented Apr 27, 2018 at 4:19

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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;

1 Comment

Please never post code-only answers on Stack Overflow. (even if you see other users doing it) What new value does your answer bring to this page? Why should researchers read/use your advice?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.