last day (15 days later) » 

11:38 PM
3
A: Trying to get property 'column_name' of non-object while displaying data on view

Vikash PathakHere is the following you are doing wrong: Replace $section->courses() with $section->courses as you are already doing early loading. And $section->courses() will query to db again. Check if relational data exists then show. So your code would be as follow: @foreach ($sections as $section) <tr>

 
@AbhishekNimbalkar let me know if you are still having any issue.
 
Your solution works but when I am retrieving data from the database its showing course name only for the first row of the table. see the image link. Each course has many sections so I want to know I have to use many to may relationship or one to many.
 
@AbhishekNimbalkar if you dd($section->courses); in view what is the output? Also as for the relationship structure... is this possible same section belongs to different course?
I mean, possible to have 1 section belongs to 2 or more courses?
@AbhishekNimbalkar then your relationship would be as follow: in your course Model hasMany sections... and in section model belongsTo course.
You don't need extra table for courses_sections... just add the course_id in sections table. And define the relationship for that.
As per the shared data: you must got the course name for the Syntax section and Introduction section... rest two are blank.
@AbhishekNimbalkar for the current senerio I have updated the ans.. You can verify now.
 
@VikasPathak After changing models, creating a new column in the table sections and updating views html <td> @php $course = $section->courses->first(); @endphp {{ $course->courseTitle or "" }} </td> am getting an error Trying to get property 'courseTitle' of non-object
 
If you have changed the model then it would be like: Just: like: @php $course = $section->courses; @endphp {{ $course->courseTitle or "" }} because now it's no longer collection.. it's just model object.
 
11:38 PM
@VikasPathak This is my Models code:- Course Model php public function sections(){ return $this->hasmany('App\Model\admin\course\section'); } and Section Model php public function courses(){ return $this->belongsToMany('App\Model\admin\course\course','sectio‌​ns','course_id'); }
 
Your relationship in section model should be public function course(){ return $this->belongsTo('App\Model\admin\course\course'); }
Just belongsTo() needed here.. and you can access this like $section->course
 
This one works but instead of course name on the table in view its showing course id.I used this code html @php $course = $section->course; @endphp {{ $course->courseTitle or "" }} screenshot link @VikasPathak dd($course); @endphp output. link
 
Now run this @php $course = $section->course; dd($course); @endphp share the output.
It must working.
 
dd($course); @endphp output. link
 
You are fine now. It should work perfactly... Your output is fine... just share the complete view code using ibb.co. Just need to debug...
 
11:38 PM
View code link link
 
It's missing the print section for course ^^ @AbhishekNimbalkar
 
I am calling course title in table html <td> i have added it on line number 26
Here`s my migration table code for creating section. html Schema::create('sections', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->Increments('id'); $table->integer('course_id')->unsigned()->index(); $table->string('title'); $table->string('sectionPosition'); $table->timestamps(); $table->foreign('course_id')->references('id')->on('courses'‌​)->onDelete('cascade‌​'); });
 
@AbhishekNimbalkar I mean, you have missed to capture screenshot for course display in your link here... ibb.co/vzXNPXc
 
I have not used any @section in the code for course I am just displaying course title in table td along with section title
 
@AbhishekNimbalkar look into screenshot.. ibb.co/vzXNPXc it's missing the @foreach loop section for $sections... also where you are display the courseTitle.
Paste your view code here.. pastebin.com > save and share the link.
 
11:38 PM
Here`s the code:- link
 
try change @php $course = $section->course; @endphp {{ $course->courseTitle or "" }} as <?php $course = $section->course; ?> {{ $course->courseTitle}} what is the output now?
 
Everything is working fine.
 
Awesome! updated solution too as per conversation.
 

last day (15 days later) »