Trying to get property 'column_name' of non-object while displaying data on view

Viewed 375

I want to show data on my blade view from relationship data but when am trying to display data whereas table contains only one row of data it's showing on view but if I insert more than one data in table it's giving me an error.

I have three tables courses, sections, course_section. In course_section table these are the following columns course_id and section_id.

I have tried {{ $section->courses()->first()->courseTitle }} this snippet in view found on stackoverflow.

My Section Model code:-

class section extends Model
{
  public function courses(){
    return $this->belongsToMany('App\Model\admin\course\course','course_sections','course_id');
  }
}

My Section Controller code:-

$sections = section::with('courses')->orderBy('id','DESC')->get();
      return view('backend.courses.section.all',compact('sections','courses'));

My view code:-

@foreach ($sections as $section)
          <tr>
            <td>{{ $section->title }}</td>
            <td>{{ $section->courses()->first()->courseTitle }}</td>
          </tr>
@endforeach

I am getting this error

"Trying to get property 'courseTitle' of non-object (View: resources/views/backend/courses/section/all.blade.php)"

2 Answers

Here is the following you are doing wrong:

  1. Replace $section->courses() with $section->courses as you are already doing early loading. And $section->courses() will query to db again.

  2. Check if relational data exists then show.

So your code would be as follow:

@foreach ($sections as $section)
<tr>
    <td>{{ $section->title }}</td>
    <td>
        @php
           $course = $section->courses->first();
        @endphp

        {{ $course->courseTitle or "" }}
    </td>
</tr>
@endforeach

Let me know if it helps!

Edited:

As per conversation the relationship has been changed like course ->hasMany -> sections and section ->belongsTo -> course so the blade would be change like.

@foreach ($sections as $section)
<tr>
    <td>{{ $section->title }}</td>
    <td>
        @php
           $course = $section->course;
        @endphp

        {{ $course->courseTitle or "" }}
    </td>
</tr>
@endforeach

Section Controller:

$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all', compact('sections'));

In the View you have to loop the sections and then the courses and create each row. For ex:

@foreach ($sections as $section)
    @foreach ($section->courses as $course)
          <tr>
            <td>{{ $section->title }}</td>
            <td>{{ $course->courseTitle }}</td>
          </tr>
    @endforeach
@endforeach

Note it's $section->courses and not $section->courses(), because the related courses are already there, you doesn't need to query them again.


Update

Or you can do the query over course

$courses = course::with('sections')->get();
return view('backend.courses.section.all',compact('courses'));

And in the View:

@foreach ($courses as $course)
    @foreach ($course->sections as $section)
          <tr>
            <td>{{ $section->title }}</td>
            <td>{{ $course->courseTitle }}</td>
          </tr>
    @endforeach
@endforeach
Related