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) "*" } } } }
dd($parents)and see what you have in each case.var_dump