Laravel Cannot use object of type stdClass as array when using Query Builder

2.9k Views Asked by At

Short Version of question:

I received Cannot use object of type stdClass as array when using Query Builder, but works fine with Eloquent. How I solve that issue?

Long Version of question:

When I using method 1 there is a error.

Error : Cannot use object of type stdClass as array (This gives for view foreach start line)

But when I using method 2 there is no error.

I wanted to know why it return error when I using method 2. How I correct it?

Method 01 (In Controller)

$parents = DB::table('stuparents');
$parents = $parents->orderBy('first_name');
$parents = $parents->get();

Method 02 (In Controller)

$parents = StuParents::orderBy('first_name');
$parents = $parents->get();

In View

@foreach($parents as $student)

//Code

@endforeach

This is the var_dump of both methods.

Method 01

object(Illuminate\Support\Collection)#316 (1) { ["items":protected]=> array(2) { [0]=> object(stdClass)#323 (6) { ["id"]=> int(2) ["first_name"]=> string(6) "Nayani" ["last_name"]=> string(10) "Kumarihami" ["student_id"]=> int(9) ["created_at"]=> NULL ["updated_at"]=> NULL } [1]=> object(stdClass)#318 (6) { ["id"]=> int(1) ["first_name"]=> string(5) "Nimal" ["last_name"]=> string(8) "Appuhami" ["student_id"]=> int(4) ["created_at"]=> NULL ["updated_at"]=> NULL } } }

Method 2

object(Illuminate\Database\Eloquent\Collection)#329 (1) { ["items":protected]=> array(2) { [0]=> object(App\StuParents)#330 (25) { ["table":protected]=> string(10) "stuparents" ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(2) ["first_name"]=> string(6) "Nayani" ["last_name"]=> string(10) "Kumarihami" ["student_id"]=> int(9) ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(2) ["first_name"]=> string(6) "Nayani" ["last_name"]=> string(10) "Kumarihami" ["student_id"]=> int(9) ["created_at"]=> NULL ["updated_at"]=> NULL } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } [1]=> object(App\StuParents)#331 (25) { ["table":protected]=> string(10) "stuparents" ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(1) ["first_name"]=> string(5) "Nimal" ["last_name"]=> string(8) "Appuhami" ["student_id"]=> int(4) ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(1) ["first_name"]=> string(5) "Nimal" ["last_name"]=> string(8) "Appuhami" ["student_id"]=> int(4) ["created_at"]=> NULL ["updated_at"]=> NULL } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } }
1

There are 1 best solutions below

0
On BEST ANSWER

I'm guessing that in the code where you have something like {{ $student['something'] }} is where the error is actually being caused.

The database query builder method get() returns an instance of Illuminate\Support\Collection where each entry is a stdClass and not an array. To access columns, use properties not indexes.

@foreach ($parents as $student)
    {{ $student->column }}
@endforeach

To quote the documentation:

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object.

https://laravel.com/docs/5.3/queries#retrieving-results