Lwin Maung Maung

My Notes and Blogs

Home Laravel Linux
<-

Laravel Relationship - avoid nested relationships

Published on  Feb 23rd, 2024Updated:  May 3rd, 2024
3 mins Read

Laravel relationship is a powerful connection between the tables of your database. Laravel has such simplicity, but we also need a good enough database structure. I will explain the connection between databases and eloquent relationships in this post.

- Laravel Eloquent Relationship (external)
- General error: 3780 Referencing column in laravel

What is a database?

    A database is a system for storing our data. It allows us to manage our data with ease. We can store the data in big query rows, which makes our database heavier, but we don't need it in our day-to-day operation.

For example, we have a bio-information of a user as follows:
 

name
email
password
address 1
address 2
state
zip
phone
emergency_contact_name
emergency_contact_phone
recovery_email
active
suspended
verified


You can put everything in a row, but we don't need everything to check the login. If a user needs to provide everything to log in each time, he will go away, too.

So, we simplify the data into two tables, users and users_information. The user table has the following information.


name
email
password
active
suspended
verified


When the client attempts to log in, he has to log in using email and password. Other fields are requirements for just saying greetings and check he violates the rule. It seems nothing different from earlier, but it will make an enormous optimization if we have millions of rows. The other data will be in user_information. We will show this information when we need to. 
    We will access information with the eloquent relationship of the User modal.

$user->information

If the user is null, the above statement will give you an error. Please make sure to use $user?->information to prevent null.

If we have many pivot tables, like assigning the school, we must put the user ID on every class he attends. Let's imagine:

There is a school. It has many courses. The courses also have many subjects, and each subject has exams and grades, as follows:

School->courses->subjects->exams->grades.

Independently, the school has many rooms to teach the subjects, like:
School->classes.

When a student enrols the course, we need a user->course pivot table. The student is to take an exam from the course. The course also has subjects and exams. The exam questions must come from courses->subjects->exam relationship. However, we can simplify the long relationship using a course_id on the exam table. Now, each course can call exam as $course->exam relationship.

When a student sits on the exam and gets the grades from the course, we will loop the course subjects and the grades for an introduction exam.
$exam will be from route-modal binding.
$user will get from Auth::user().

In the controller, the course and the exam will match against the user. If it does not match, it will abort. If these data matches, we continue to blades.

//
abort_unless(Auth::user()->course === $exam->course,404);
$grades = Auth::user()->grades()->where('exam',$exam_id)->get();
//

In our grades table, we must have:

- student_id
- exam_id
- subject_id,
- grade

In blade,

//
@forelse($grades as $grade)
    {{$grade->subject}} - {{$grade->grade}}
@empty
Nothing to display
@endforelase
//

Above example, we need the relationships as follows:
Grade - subject relationship
Grade results
The exam and user must be from Route-modal binding and Authentication.

In conclusion, we don't have to make nested relationships to call the table we like to use. We can reference the field we would like to use for the results and get the results.

 

© lwinmaungmaung MMXXIII. All Rights Reserved.