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)"