ManytoMany Relation between 3 tables Laravel - laravel

I have 3 tables with Models all belongToMany related with each other:
Batchyear
Grade
Student
A student may attend many batchyears, and will be have many grades, and each grade have many students.
How to get the relationship via Eloquent so that a student is in a certain grade in a certain batchyear?
class Batch extends Model
{
public $guarded = [];
public function students()
{
return $this->belongsToMany(Student::class);
}
public function grades()
{
return $this->belongsToMany(Grade::class);
}
}
and here is grade
class Grade extends Model
{
public $guarded = [];
public function students()
{
return $this->belongsToMany(Student::class);
}
public function batches(){
return $this->belongsToMany(Batch::class);
}
}
and students
class Student extends Model
{
public $guarded = [];
public function batches()
{
return $this->belongsToMany(Batch::class)->withPivot(['batch_id']);
}
public function grades()
{
return $this->belongsToMany(Grade::class);
}
}
tables students . batches. grades. batch_student, batch_grade. and grade_student . dont solve the problem. if I select a grades student It shows all students even who are in previous batch in this grade. need a table like student_grade_batch. so 3 foreign keys.

Related

Laravel Model Relationship between Students and Courses

I have a Model Student and Course.
Here's what I am trying to get from those models,
Every student is enrolled to one course and the way I save it to the database is like below,
Student Info..., course_id... <- which is basically taken from the Courses table
I want to retrieve the Course name since I already have the course_id stored in the Students table.
I designed the model like:
//Course Model
class Course extends Model
{
public function student(){
return $this->belongsToMany('App\Student');
}
}
//Student Model
class Student extends Model
{
public function course(){
return $this->hasOne('App\Course');
}
}
But I don't know how to or what to do next, I am really new to Eloquent approach.
Need help.
I tried to retrieve the Course name using the code below :
$students = Student::all();
#foreach($students as $student)
$student->course->name
....
but I think it is wrong, I'm only getting an error about unknown column
Instead of using belongsToMany for student relation in Course.php model,
Use hasMany function like:
//Course Model
class Course extends Model
{
public function student(){
return $this->hasMany('App\Student');
}
}
//Student Model
Since, student belongs to course, you should use belongsTo instead of hasOne
class Student extends Model
{
public function course(){
return $this->belongsTo('App\Course');
}
}
In order to optimize the query for retrieving all related courses, you can use with Eagar Loading relationship like:
$students = Student::with('course')->all();
#foreach($students as $student)
//If incase there are no course related to the student
$student->course->name ?? null
....
Previous Code :
//Course Model
class Course extends Model
{
public function student(){
return $this->belongsToMany('App\Student');
}
}
//Student Model
class Student extends Model
{
public function course(){
return $this->hasOne('App\Course');
}
}
Replace with :
//Course Model
class Course extends Model
{
public function students(){
return $this->hasMany(Student::class); // '\App\Student' also ok
}
}
//Student Model
class Student extends Model
{
public function course(){
return $this->belongsTo(Course::class);
}
}
Why belongsTo at Student Model ?
Inside : When a table primary key assign in another table as foreign key then you should add belongsTo this assigning table.
Here Courses table primary id assigned as foreign key at Students table

Laravel eloquent relationship with 2 foreign keys

There are 2 tables and 2 models. Table "games" has 2 foreign keys "team_a_id" and "team_b_id"
I try to define relationship but can't understand how build this relationship. One game can have only one team A and only one team B. But one team can have many games.
At this moment I have
class Game extends Model
{
public function teamA()
{
return $this->hasOne('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->hasOne('App\Models\Team', 'team_b_id');
}
}
and
class Team extends Model
{
public function gameTeamA()
{
return $this->belongsTo('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->belongsTo('App\Models\GameSerie', 'team_b_id');
}
}
I have to define relationship in order to find all games where team was team_a or team_b.
e.g.
$allGames = $team->games($id);
Also i am not sure that i defined relationships right
It's the other way around. The model with the foreign key has the BelongsTo relationship:
class Game extends Model
{
public function teamA()
{
return $this->belongsTo('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->belongsTo('App\Models\Team', 'team_b_id');
}
}
class Team extends Model
{
public function gameTeamA()
{
return $this->hasOne('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->hasOne('App\Models\GameSerie', 'team_b_id');
}
}

Dependencies between School, Student and Lesson models

I have a School model that has many Student models who attend to multiple Lesson models, I also have a controller for each one.
I need the ability to access the school type (large, small, etc...), whether I am in the Student, Lesson or School controller.
Would this approach be correct in a strict OOP world?
// School model
class School
{
...
public getSchoolType()
{
return $this->schoolType;
{
}
// Student model
class Student
{
...
public school()
{
return $this->school;
{
}
// Lesson model
class Lesson
{
...
public student()
{
return $this->student;
{
}
// Student controller
class StudentController
{
public function show(Student $student)
{
$schoolType = $student->school->schoolType;
return view('students', array($schoolType));
}
}
// Lesson controller
class LessonController
{
public function show(Lesson $lesson)
{
$schoolType = $lesson->student->school->schoolType;
return view('lessons', array($schoolType));
}
}
If lessons relate to students in a Many-to-Many way, how do I get the schoolType within the Lesson controller if there is no students attending that Lesson?
My point is, should I actually get the schoolType through the Student model like $lesson->school->schoolType or it should be more like $lesson->student->school->schoolType, so lessons simply don't relate directly to students?
use eloquent relationship hasMany and belongsTo.
In your School Model-
public function students()
{
return $this->hasMany(Student::class);
}
In you Student model-
public function lessons()
{
return $this->hasMany(Lesson::class);
}
public function school()
{
return $this->belongsTo(School::class);
}
In your Lesson Model-
public function student()
{
return $this->belongsTo(Student::class);
}
public function school()
{
return $this->belongsTo(School::class);
}
now you can easily access schoolType as you can traverse to model to model using this relationship. An example would be, In your Lesson Controller-
$lession = Lession::find($lession_id);
$schoolType = $lession->student->school->schoolType;
If you want to access schoolType directly from Lesson-
$lession = Lession::find($lession_id);
$schoolType = $lession->school->schoolType;

Laravel 5.4 table relationship between 3 small tables

I have a recently started learning Laravel 5.4, I am having trouble with my 3 way table relationship, I've looked at a few articles online regarding many to many, but this relationship is just a "hasOne" on both sides.
Could anyone give me a helpful hint as to how to structure my table relationship, here is the PK/FK relationship:
Users table (id)
Listings table (id, user_id)
Insights table (id, listing_id) - one insight row per listing only.
And the models below:
Users Model
class User extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
And what I am trying to achieve is to query the users own listings, with each listings current insights.
Thanks a bunch.
Simon.
User model
class User extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
And if you want query users with Listing and Insight
$users = User::with(['listing', 'listing.insight'])->get();
foreach($users as $user) {
$user->listing->insight;
}
class User extends Model
{
public function listing()
{
return $this->hasMany(Listing::class);
}
}
class Listing extends Model
{
public function insight()
{
return $this->hasOne(Insight::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
class Insight extends Model
{
public function listing()
{
return $this->belongsTo(Listing::class);
}
}
$users = User::with('listing.insight')->get();
foreach($users as $user) {
$user->listing->insight;
}

Laravel Eloquent Relationship Through Another Table

I have the following database tables:
Seasons
id
number
Teams
id
name
Standings
id
season_id
team_id
The question is, how could I get all of the teams in a season through the standings table. At the moment I am getting all of the teams this way:
$teams = [];
$standings = $season->standings;
foreach($standings as $standing){
$teams[] = $standing->team;
}
Is there a way I could do this using Eloquent relationships? I have tried HasManyThrough with no success. These are what my models look like currently:
class Season extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent{
public function team(){
return $this->belongsTo('Team');
}
}
class Team extends Eloquent{
public function standings(){
return $this->belongsToMany('Standing');
}
}
Your relationships look a little off. Here is all the relationships you should need though only the belongsToMany ones are required for this specific scenario of finding all the teams in a season.
class Season extends Eloquent {
public function teams()
{
return $this->belongsToMany('Team', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Team extends Eloquent {
public function seasons()
{
return $this->belongsToMany('Season', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent {
public function team()
{
return $this->belongsTo('Team');
}
public function season()
{
return $this->belongsTo('Season');
}
}
You would use the belongsToMany relationship rather than a hasManyThrough to query all the teams in a season. That would look something like...
Season::with('teams')->find($season_id);
foreach($season->teams as $team) {
echo $team->name;
}

Resources