Home » Php » javascript – Trying to get property of non-object when display value of json response in Laravel

javascript – Trying to get property of non-object when display value of json response in Laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

i try to display the value of json response . Its work well when I dd @php dd( $user->irel__com_access_level->ID); @endphp inside blade view but when i try to display at table it return error which is

Trying to get property of non-object

My code display in table

     @foreach($content as $user)
      <td>{{ $user->irel__com_access_level->ID }}</td>
     @endforeach

Here is my json response ….i want to display ID which is inside irel__com_access_level

{
    "data": [
        {

        {
            "ID": "banana",
            "PWD": "W6ph5Mm5Pz8GgiULbPgzG37mj9g=",
            "NICK_NAME": "BANANA NANA NA",
            "PWD_INVALID_HIT": "0",
            "PWD_CREATE_DT": "2019/09/06 16:44:40",
            "INSTITUTION_ID": "C01",
            "ACL_ID": "L03",
            "LOGIN_DT": "                   ",
            "LOGIN_STS_ID": "N",
            "STS_ID": "C03",
            "TYPE_ID": "U00",
            "UPD_ID": "asmidah",
            "UPD_DT": "2019/09/06 16:44:40",
            "EMAIL_ID": "[email protected]",
            "PHONE_NO_ID": "0",
            "HP_ID": "0101234567               ",
            "CRT_DATE_DELETED": "2019/09/06 16:44:40",
            "irel__com_access_level": {
                "ID": "L03",       // i want to display this
                "DESCRIPTION": "2NDLEVEL",
                "IS_CREATE": "N",
                "IS_READ": "Y",
                "IS_UPDATE": "Y",
                "IS_DELETE": "N",
                "STS_ID": "R01",
                "UPD_ID": "shukrimb",
                "UPD_DT": "2012/09/13 13:28:25"
           }

my controller

 private $client;

    public function __construct(){


        $this->client = new Client(['base_uri' => 'http://172.19.52.6/api/configuration/getUserIndex']);

    }

    public function index()
    {

        $response =  $this->client->get('getUserIndex');
         $content = json_decode($response->getBody());

         return view('configuration.comuserprofiles.ComUserProfilesList', ['content' => $content->data]);


i want to display ID which is inside irel__com_access_level in json response

How to&Answers:

Try this

 return view('configuration.comuserprofiles.ComUserProfilesList', ['content' => $content->data->first()]);

or

 return view('configuration.comuserprofiles.ComUserProfilesList', ['content' => $content->data()->first()]);

Answer:

Just already solved this answer by adding isset

 <td>    @if(isset($user->irel__com_access_level))


         @if(trim($user->ACL_ID) == trim($user->irel__com_access_level->ID))
              {{ $user->irel__com_access_level->DESCRIPTION }}   
         @endif

        @endif

     </td>