Laravel Error Message Cannot use object of type stdClass as array

Posted on

In this article, the error as specified in the title of this article will be discussed further using the case or the example extracted from a web page application powered by a Laravel framework. The Laravel Error Message specified is ‘Cannot use object of type stdClass as array‘. This error itself has something to do and it correlates with another article titled ‘Laravel Error Message Call to undefined method stdClass::toArray()‘ in this link.

This is the error which is shown in the form of an image :

Basically, there is a variable which is passed from the controller named RoleController.php to a blade view template file named ‘edit.blade.php’. Below is the snippet code which is indicating the passing process of a variable to the view file template in the RoleController.php file :

public function edit($id_role) {

        $edited_role = DB::table('role')->where('id_role',
         '=', $id_role)->first();

        return view('role.edit')->with('edited_role', $edited_role);
    }

As shown in the above snippet code, there is a variable named $edited_role is passed through the controller to a blade view template file named ‘edit.blade.php’ inside a folder named ‘role’ which is located in the view folder in the laravel framework which is usually stored in the ‘resources/views’. Below is the directory tree which is describing the location of the ‘edit.blade.php’ file using the ‘tree’ command in Linux operating system :

user@hostname:/var/www/html/laravel-project$ tree resources/
resources/
...
└── views
    ├── role
    │   ├── edit.blade.php
    │   └── index.blade.php
...

In this case, the error itself is actually happened in the blade view template file as shown in the following snippet code :

<div class="form-group">

<label class="col-md-3 form-label text-right" for="nameField">Name : </label>
<div class="col-md-6"><input class="form-control" name="name" type="text" value="{{ $edited_role['name'] }}" placeholder="Name" /></div>
&nbsp;

</div>

As it can be seen in the above snippet code, to be able to extract or to print the value of the ‘role’ name which is going to be edited further in the edit form, the code which is used is ‘{{ $edited_role[‘name’] }}’. It is a standard code for printing the value possessed by an associative key named ‘name’ inside the variable named ‘$edited_role’. That is when the error is finally triggered.

The error is ‘Cannot use object of type stdClass as array’. Based on the error itself, it can be fathomed and it is true that the variable which is passed in the form of class object named ‘edited_role’ from the controller file named ‘RoleController.php’ is printed in the way of an associative array should be printed. In other words, there is no way the object of the class retrieved from the query process executed from a table will be treated as an array.

To solve the problem and correct the error, the following part of the snippet code must be changed :

<div class="col-md-6"><input class="form-control" name="name" type="text" value="{{ $edited_role->name }}" placeholder="Name" /></div>

So, in order to be able to summon or to call the value of a certain field or attribute from a standard object model variable is by writing in this way ‘{{ $edited_role->name }}’.

One thought on “Laravel Error Message Cannot use object of type stdClass as array

Leave a Reply