Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add to my Index/Show, so that it displays a table, from another table. (Laravel)

First post here, hope all is well with everyone! I am working on my senior project and struggling with this concept. Maybe it isn’t possible, or it is and I’m thinking about it the wrong way.

I currently have this- This displays the student, with all its foreign keys in the other tables. In settings, there is a foreign key ‘conversion_id.’, I would like settings on the student call(code below) to ALSO display the conversion table, from the FK in settings.

$student = Student::with('studentIntroSurveys', 'settings', 'giftsSurveyResults', 
'studentGiftSurveys', 'devotionals', )->get();

If this does not make sense, I am sorry. I am still l new to the language.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(Code to display students. It shows settings, but not the conversions within settings.)

$student = Student::with('studentIntroSurveys', 'settings', 'giftsSurveyResults',
'studentGiftSurveys', 'devotionals', )->get();

if (!$student) {
    return response('No Data', 400);
} else {
    return response($student);
}

I would like it to display this settings, but WITH the conversion_id table!!
What it displays =

settings: { setting_id: 4, student_id: 1, dark_mode: 1, conversion_id: 1, 
notification_enabled: 1, notification_time: "08:00:00" } `

my has-one method =

public function settings()
{
    $settings = $this->hasOne(Settings::class, 'student_id', 'student_id');
    return $settings;
}

>Solution :

You can use the dot syntax for nested eager loading: 'settings.conversation' e.g.

$student = Student::with('studentIntroSurveys', 'settings.conversation', 'giftsSurveyResults', 'studentGiftSurveys', 'devotionals', )->get();

Alternatively, you could set up a belongsToMany relationship on the Student model and use settings as the pivot table:

public function conversations()
{
    return $this->belongsToMany(Conversation::class, 'settings')
}

Just a few FYIs:

Your if statement is never going to return the 400 response as $student is always going to be a collection. You could instead do if ($student->isEmpty()).

I would also recommend changing the variable to $students as it will be a collection of students rather than a single student.

You can simple your settings relationship by removing the temporary variable and just returning the relationship:

public function settings()
{
    return $this->hasOne(Settings::class, 'student_id', 'student_id');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading