Orgil's avatar
Level 2

Undefined property: stdClass

I'm trying to get result from query in laravel

SELECT c.title FROM courses c WHERE c.schools_id = 1 AND c.grade_levels_id = 2 AND c.id NOT IN (SELECT u.course_id FROM course_student u WHERE u.user_id = 4)

Above sql query works fine

But when using query builder it doesn't work

$courses = DB::table('courses')
                 ->join('schools', 'schools.id', '=', 'courses.schools_id')
                 ->join('subjects', 'courses.subjects_id', '=', 'subjects.id')
                 ->join('grade_levels', 'grade_levels.id', '=', 'courses.grade_levels_id')
                 ->join('users', 'users.id', '=', 'courses.users_id')
                 ->join('periods', 'periods.id', '=', 'courses.periods_id')
                 ->join('course_marking_period', 'course_marking_period.course_id', '=', 'courses.id')
                 ->join('marking_periods', 'course_marking_period.marking_period_id', '=', 'marking_periods.id')
                 ->where('courses.schools_id', '=', 1)
                 ->where('courses.grade_levels_id', '=', 1)
                 ->whereNotIn('courses.id', function($q) use ($id)
                 {
                   $q->select('course_id')->from('course_student')->whereRaw('course_student.users_id = ?', array($id));
                 })->get();

Error is : Undefined property: stdClass::$subjects which is as follows

@foreach($courses as $course)
                      <tr class="even pointer" id="row_course{{$course->id}}">
                        <td class=" ">{{$course->id}}</td>
                        <td class=" ">{{$course->title}}</td>
                        <td class=" ">{{$course->subjects->title}}</td>
                        <td class=" ">{{$course->grade_levels->title}}</td>
                        <td class=" ">{{$course->users->firstname}}</td>
@endforeach
0 likes
4 replies
Snapey's avatar

dd ($course)?

I don't see how subject becomes an object? Why is it not {{$course->subject.title}} ?

Orgil's avatar
Level 2

dd($courses) returns as follows

array:2 [▼
  0 => {#406 ▼
    +"id": 2
    +"title": "PM"
    +"total_seats": 5
    +"room": "101"
    +"required": 1
    +"days": "1,2,3"
    +"subjects_id": 1
    +"users_id": 6
    +"schools_id": 1
    +"grade_levels_id": 1
    +"periods_id": 1
    +"created_at": "2016-10-07 13:26:43"
    +"updated_at": "2016-10-07 13:26:43"
    +"name": "Өвөл"
    +"address": "Ulaanbaatar, SBD 3-r khoroo"
    +"phone": 12345678
    +"email": "[email protected]"
    +"system_codes_id": 1
    +"principle": "Нэргүй"
    +"bank_account": ""
    +"city": "Ulaanbaatar"
    +"programs_id": 1
    +"user_profiles_id": 3
    +"firstname": "Begzee"
    +"lastname": "Togoldor"
    +"birthday": "17-01-1990"
    +"gender": "Эрэгтэй"
    +"register_id": "УЖ780512"
    +"degrees_id": 2
    +"remember_token": null
    +"length": 90
    +"start_time": "7:40"
    +"end_time": "9:20"
    +"course_id": 1
    +"marking_period_id": 2
    +"start_date": "31-01-2016"
    +"end_date": "15-02-2016"
    +"post_start_date": "08-02-2016"
    +"post_end_date": "08-02-2016"
    +"start_register": "08-02-2016"
    +"end_register": "01-02-2016"
  }
  1 => {#407 ▼
    +"id": 1
    +"title": "PM"
    +"total_seats": 5
    +"room": "301"
    +"required": 1
    +"days": "1"
    +"subjects_id": 1
    +"users_id": 6
    +"schools_id": 1
    +"grade_levels_id": 1
    +"periods_id": 1
    +"created_at": "2016-10-07 13:26:37"
    +"updated_at": "2016-10-07 13:26:37"
    +"name": "Зун"
    +"address": "Ulaanbaatar, SBD 3-r khoroo"
    +"phone": 12345678
    +"email": "[email protected]"
    +"system_codes_id": 1
    +"principle": "Нэргүй"
    +"bank_account": ""
    +"city": "Ulaanbaatar"
    +"programs_id": 1
    +"user_profiles_id": 3
    +"firstname": "Begzee"
    +"lastname": "Togoldor"
    +"birthday": "17-01-1990"
    +"gender": "Эрэгтэй"
    +"register_id": "УЖ780512"
    +"degrees_id": 2
    +"remember_token": null
    +"length": 90
    +"start_time": "7:40"
    +"end_time": "9:20"
    +"course_id": 3
    +"marking_period_id": 1
    +"start_date": "31-01-2016"
    +"end_date": "15-02-2016"
    +"post_start_date": "08-02-2016"
    +"post_end_date": "08-02-2016"
    +"start_register": "08-02-2016"
    +"end_register": "01-02-2016"
  }
]

So how to apply above array to this

@foreach($courses as $course)
                      <tr class="even pointer" id="row_course{{$course->id}}">
                        <td class=" ">{{$course->id}}</td>
                        <td class=" ">{{$course->title}}</td>
                        <td class=" ">{{$course->subjects->title}}</td>
                        <td class=" ">{{$course->grade_levels->title}}</td>
                        <td class=" ">{{$course->users->firstname}}</td>
@endforeach

If i write it as eloquent it is working. However i need to do it with query builder for what i need. It seems relationship not working if i write like this

<td class=" ">{{$course->id}}</td>
                        <td class=" ">{{$course->title}}</td>
                        <td class=" ">{{$course->title}}</td>
                        <td class=" ">{{$course->grade_levels_id}}</td>
                        <td class=" ">{{$course->users_id}}</td>
                        <td class=" ">{{$course->periods_id}}</td>
                        <td class=" ">{{$course->days}}</td>
                        <td class=" ">{{$course->room}}</td>
                        <td class=" ">{{$course->total_seats}}</td>

It is working. So how to apply relationship with above query builder.

Snapey's avatar

You are not using eloquent models so can't leverage any relationship functionality.

If grade_levels is a simple list, you could load it as a separate variable then index it with your grade_levels_id field (and exclude it from the query)

I see you join to the grade_levels table, but the problem could be that you have conflicting field names, $title in the course table and $title in the grade_levels table.

(same problem with the subjects)

InaniELHoussain's avatar
@foreach($courses as $course)

    <PHPTAG> $subject = Subject::findOrFail($course->subjects_id); </PHPTAG>  // Do the same for the dependcies grade and user

                  <tr class="even pointer" id="row_course{{$course->id}}">
                    <td class=" ">{{$course->id}}</td>
                    <td class=" ">{{$course->title}}</td>
                    <td class=" ">{{$subject->title}}</td>
                    <td class=" ">{{$grade_levels->title}}</td>
                    <td class=" ">{{$user->firstname}}</td>
@endforeach

Please or to participate in this conversation.