Laravel and pivot table to relate different model - laravel

I'm wondering how, but it's bit confusing.
I have fine belongs to many relation between users and groups tables as well as appropriate models for all of that.
But i also have table students, where not all users are student so i students table i maintain user_id field.
My question would be: Can i use pivot table "group_user" for relations between student and group model, in students table i have "user_id" field? and how?
I tried something like
public function students()
{
return $this->belongsToMany('Student','group_user','group_id','user_id');
{
but i don't see the way how to tell eloquent not to take students.id but to take students.user_id???

Assuming these relations:
Subject belongsTo Group
Group belongsToMany User
User hasOne Student
you can easily do this:
$subject = Subject::find($someId);
// of course for multiple subject use eager loading:
// $subjects = Subject::with('group.users.student')->get();
$users = $subject->group->users; // related users
foreach ($users as $user)
{
$user->student; // null|student model
}

Related

Invalid argument supplied for foreach() in laravel when use Eloquent: Relationships

I wanna get the class name of a user via the user ID. When I input the ID of a user so I will wanna get the class name. I have three tables such as users table, classes table, and class_users table. The class_users table is born from two users table and classes table.
A users table has an id, name, email, password.
A classes table has an id, class_code, class_name.
A class_users table has an id, class_id, user_id
And this problem relates to Eloquent Relationships.
Thank you for help.
My Route:
Route::get('/find_classes/{id}','StudentController#find_classes');
My Controller function:
public function find_classes($id)
{
$users = User::find($id);
foreach($users->classes as $class)
{
echo $class->name . '<br';
dd($class);
}
}
My User Model:
public function classes()
{
return $this->belongsTo('App\Classes','class_users','user_id','class_id');
}
Looks like you might have the wrong relationship set up on your User model. You have a one to many set up, but your DB is setup to handle a many to many. I suggest you change your User model relationship:
public function classes()
{
return $this->belongsToMany('App\Classes');
}
Note, you may need to name the FK on that relation, as I see you have class_id on the table, but your actual class is named 'Classes'. Check through your relationships to ensure this is explicit on the FK where it doesn't follow Laravel convention exactly.
With this relationship, your foreach loop should work. It would be a good idea for efficiency, as mare96 noted, to eager load the classes on the $users collection when you query:
$users = User::with('classes')->find($id);

Laravel Reference a One-to-Many Table through a Many-to-Many Relationship

I am having the most difficult time with this, and I'm not sure if the issue is the relationship or what. I have a 'users' table that has a many to many relationship (via a pivot table) with a 'practices' table. Practices, however, have a one-to-many relationship with a 'doctors' table, in which a practice can have many doctors, but a doctor can only belong to one practice. I also then have a many-to-many relationship between the doctors and a 'patients' table, which I would need to run counts and count-by-date sort of queries off of.
Currently, it is set to where a user can see what doctors they have by running a foreach loop on practices, and then on $practices->doctors. But this is not optimal, as the doctors then can't be sorted alphabetically and such. Can someone help me figure out how I can reference the doctor directly without needing the additional foreach loop?
This is my current code. Thanks in advance!
dashboard.blade.php
#foreach ($practices as $practice)
#foreach ($practice->doctors as $doctor)
Doctor Name: {{$doctor->full_name}}
Patient Count: {{$doctor->patients()->count()}}
#endforeach
#endforeach
DashboardController.php
$practices = User::find(Auth::user()->id)->practices();
return view('dashboard')->withPractices($practices);
there is no predefined eloquent relationship for your scenario. but we can create a relationship using another relationship.
I believe you have already created the relationship to 'practices' in the 'User Model' and 'doctors' in 'Practice Model'.
// in User Model
public function practices()
{
return $this->belongsToMany(Practices::class);
}
// in Practice Model
public funcion doctors()
{
return $this->hasMany(Doctor::class);
}
You need a 'doctors' relationship in the 'User Model'.
// in User Model
public function doctors()
$this->load(['practices.doctors' => function($query) use (&$relation) {
$relation = $query;
}]);
return $relation;
}
now you can get 'doctors' for a 'user' like this.
$doctors = User::find(Auth::user()->id)->doctors;
You can create a direct relationship by "skipping" the practices table:
class User extends Model {
public function doctors() {
return $this->belongsToMany(
Doctor::class,
'practice_user',
null,
'practice_id',
null,
'practice_id'
);
}
}

Laravel pivot with multiple columns

Hi I have a problem with Laravel`s pivot table.
I have the following tables: students, courses and lessons.
The table lessons is connected with courses through a foreign key courses_id, and the tables students and courses are connected through a pivot courses_students.
So I can access the information through students like this:
//Students model
public function courses()
{
return $this->belongsToMany(Courses::class,'courses_students','student_id', 'course_id')
->with('lessons');
}
//Courses model
public function lessons()
{
return $this->hasMany(Lesson::class);
}
This works completely fine for this kind of relationship, but I want to add a third column in the pivot with name lesson_id for the lessons table.
I am doing this because, sometimes I need to get a specific set of lessons from each course for each user.
I succeeded in doing so, by using a model courseStudent for the pivot table.
Using the model for pivot my calls became like this.
Student->with('courseStudent.courses')
->with('courseStudent.lessons')
->get();
This partially does what I need it to do, but I want to maintain the relation ship between courses and students.
Is there a way to achieve that?
Example from docs(go through Many To Many):
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
Pivot table is meant to use belongsToMany relationship on both entities.
So your students and courses should have it defined if you want pivot table between that is using eloquent default capacity.
As a side note pay attention on naming convention because that way you will reduce issues on minimum: pivot table should be tableanamesingular_tablebnamesingular where order is set by alphabetical order of tables' names (i.e. post_user Yes, user_post No).
Id fields in pivot table should be tablenamesingular_id.
You can set names however you want but this way you will have less unepected behavior in future using eloquent. All of this you have in documentation page and I recommend you go through it thoroughly.
Other way is to use dynamic properties for getting certain values. Example from docs:
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
If you would like to manually change values in pivot table, you should create separate model for it that would be connected with that entity/table (pay attention that pivot model extends Pivot as in example from docs rather than Model):
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class PostUser extends Pivot
{
// other definitions related
}
You can use join for third relation:
public function courses(){
return $this->belongsToMany(Courses::class,'courses_students','student_id', 'course_id')
->withPivot('lesson_id')
->join('lessons','lesson_id','=','lessons.id')
->select('lessons.id','lessons.title', ...);
}
If you are going to use the same pivot table for courses and lessons, you can to do something like this:
//Students model
public function courses()
{
return $this->belongsToMany(Courses::class,'courses_students','student_id', 'course_id')
->whereNotNull('course_id');
}
public function lessons()
{
return $this->belongsToMany(Lessons::class,'courses_students','student_id', 'lesson_id')
->whereNotNull('lesson_id');
}
Then just use it:
$courses = $student->courses;
$lessons = $student->lessons;

How to compact two relationship in one relationship

I have some issue when I wanna compact two relationships in Laravel.
There were 3 tables with model.
table1: episodes --> model: Episode
relations -- hasMany --> posts
-- belongsToMany --> users
pivot-table2: posts --> model: Post
columns -- episode_id
-- user_id
relations -- belongsTo --> user
-- belongsTo --> episode
table3: users --> model: User
Yes, It was a many-to-many relationship. We could get posts or users relationships on Episode model.
If I have a single model collection
$episode = new App\Episode
How could it eager load like
$episodes = new App\Episode::with('posts.user').
Maybe I could do like below
$episode = new App\Episode::with('posts.user')->where('id','=','targetId')
But It seems like would first join three tables then make a query to get target episode.
Is there any way works like
$episode = new App\Episode::find(targetId)-> with('posts.user')
If you are using posts table just as pivot table, you are doing it wrong.
Episode Model:
public function users()
{
return $this->belongsToMany('App\User', 'posts');
}
// I would name my pivot table different than posts, if you don't pass the second parameter Laravel will look for episode_user table
User Model:
public function episodes()
{
return $this->belongsToMany('App\Episode', 'posts');
}
In your controller:
$user = User::find(1);
$user->episodes();
$episodes = Episode::find(1);
$episodes->users();

laravel one-to-many get data without inner join

I am trying to get a feel around the laravel ORM and I have the following models.
I have a:
user table with- id, firstname, lastname
city table with - id, name
usercity table with - user_id, city_id
The usercity table tracks the cities the user has visited.
I added the following in city model:
public function usercity()
{
return $this->hasMany('App\UserCity');
}
And another function in user model
public function usercity()
{
return $this->hasMany('App\UserCity');
}
I also added a model for UserCity and added following function there.
public function city()
{
return $this->belongsTo('App\City');
}
public function user()
{
return $this->belongsTo('App\User');
}
Now, the goal is to retrieve all the cities a user has visited. I used the following function.
$usercities = User::where('id','=',1)->first()->usercity()->get();
This works in the sense that it retrieves the user_id and city_id.
What would i need to do to get all the fields in the city table also?
Current response:
[[{"user_id":"1","city_id":"1"},{"user_id":"1","city_id":"2"},{"user_id":"1","city_id":"3"},{"user_id":"1","city_id":"4"}]]
I might be able to use inner join but I wanted to see if there was another way to retrieve the data which safely populates the data for me.
What you really have is a many-to-many relationship between users and cities, with the usercity table being the pivot table. Laravel uses the BelongsToMany relationship to implement this. You'll need to make a few changes to get this to work.
In your city model:
public function users() {
return $this->belongsToMany('App\User', 'usercity');
}
In your user model:
public function cities() {
return $this->belongsToMany('App\City', 'usercity');
}
You can get rid of the UserCity model. There is usually no reason to need a model for the pivot table.
The usercity table may need to be updated to add an id field as the primary key. I've not tried it without one, however, so it may work as you have it. Also, if you wanted, you could rename the table to city_user to conform to Laravel conventions, and then you wouldn't need to specify the table name in the relationship definitions.
Once your relationships are setup correctly, you can access a user's cities via the cities relationship on the user, and you can access a city's users via the users relationship on the city. For example:
// all of the cities visited by user 1
$user = User::find(1);
$usercities = $user->cities;
// all of the users that have visited city 1
$city = City::find(1);
$cityusers = $city->users;
You can find more information about the relationships in the documentation here.

Resources