John Davidson

php - how to file get with base url in laravel 8 using relation

0 comments
Message:


$data = Course::with('subject' , 'module' , 'lession' ,'lecture_audio' , 'lecture_text', 'lecture_document', 'lecture_image' ,'lecture_other_video' , 'course_video' , 'video' ,'add_assessment' , 'add_homework','text_homework' , 'video_homework')->where('id' , $request->course_id)->get();
return $data;


My Answer:
In Laravel 8, you can use the `with()` method to eager load relationships along with the base model. Here is an example of how you can fetch a Course along with its related models using the `with()` method:

php
$data = Course::with('subject', 'module', 'lession', 'lecture_audio', 'lecture_text', 'lecture_document', 'lecture_image', 'lecture_other_video', 'course_video', 'video', 'add_assessment', 'add_homework', 'text_homework', 'video_homework')
->where('id', $request->course_id)
->get();

return $data;


In this example, we are fetching a Course model along with its related models like Subject, Module, Lession, etc. The `with()` method accepts an array of relationship names that you want to eager load.

Make sure to define the relationships in your Course model using the `hasMany()` or `belongsTo()` methods to establish the relationships between the models.

Rate this post

5 of 5 based on 8697 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us