I have a users table and consultant_profile table set up on my db. They are related and in my controller action i can see in the users object(from a model) that it has a relations array and in there is the profile object.
I am now trying to create a table for qualifications which has a one to many relationship with the consultants profile so one profile can have many qualifications.
For ease i here is a shorter version of the schema for the tables:
consultant_profile [
- id
- address line 1
- postcode
]
consultant_qualifications [
- id
- consultant_profile_id
- qualification
]
I have put this in my model for the consultant_profile
public function qualifications()
{
return $this->hasMany('ConsultantQualifications', 'consultant_profile_id');
}
and this in my consultant_qualifications model
public function profile()
{
return $this->belongsTo('ConsultantProfile',null,"consultant_profile_id");
}
I have tried it with putting in foreign/local keys and without.
What i expect the out come to be is i can look in the relations array of user object and see the profile object, i could then look into profile object and in its relations array i should see qualifications object
But i don't, any reason why?
Related
Could anyone please tell me how to create a relation between 3 tables.I have 3 tables. User, Profile and Post. Now I want to list all posts on the home page with user name form users table and profile image from the profiles table and posts from posts table.
There are few things to consider before suggesting any code. Your database structure, eloquent model classes and any other condition you want to apply before getting posts from database.
Hope, your profiles and posts table (both) contains user_id field.
You have to define two relationships.
In Post model define:
public function user() {
return $this->belongsTo('App\User');
}
In User model define:
public function profile() {
return $this->hasOne('App\Profile');
}
Now, before forwarding your $posts to view you may run this statement:
$posts->load('user.profile);
This will load relevant user and their profile within posts. Like this:
posts:[
{...,
user:{
...,
profile:{...}
}
},
...
]
Further reading recommended:
Belongs To Relationships
Nested Eager Loading
I am storing information on a 4-day class. The information is dates 1-4 and each date has an instructor associated with it as there can be different instructors on different days. Let's just talk about day_1 and instructor_day_1 for now. My models are as follows:
Course Model:
public function instructor()
{
return $this->belongsTo('App\Instructor');
}
Instructor Model:
public function course()
{
return $this->hasMany('App\Course');
}
When i go to the #show method in my controller, i find the correct course but when i try to load the view, i can't figure out how to access the foreign relation. Right now i'm trying:
<p class="card-text">{{$course->day_1}} assigned to {{$course->instructor_day_1->instructor->name}}</p>
But that's not yielding anything but errors. If i do {{$course->instructor_day_1}}, i get the correct value from the database.
Is my relationship backwards? Does Course model need be "belongsToMany"? What is the correct syntax for pulling the instructor info? Do i need to specify any foreign key relations in my models?
belongsTo is the correct relationship, but it's not set up correctly. And you'll need 4 of them, 1 for each foreign key on the courses table.
This should get you started:
public function instructorDay1()
{
return $this->belongsTo('App\Instructor', 'instructor_day_1');
}
And then you would call it like this, to get instructor's name:
$course->instructorDay1->name;
And you'll need to do this for all 4 relationships.
I am working in laravel5.4. I have created four table as ticket, users, and company. Here, I have stored users id in ticket table. And stored company id in users table.
Here, I want to show ticket's user name with that users company name.
Here, I have create relation for it that looks like below.
Ticket model :
public function requesters(){
return $this->belongsTo('App\User','requester_id');
}
Here requester_id is who create ticket.
Users model :
public function company()
{
return $this->belongsTo('App\Models\Admin\Company');
}
Company model :
public function Users()
{
return $this->hasOne('App\Users');
}
Here, I have written query to get users information that looks like below.
Ticket::with('requesters')->orderBy('subject','asc')->paginate(10);
Now, I want to fetch company information or request_id So what changes should I have to do in this query to fetch company info along with ticket and users ?
If you want to to Eager load multiple relationship you can put them all in single with like this:
Ticket::with('requesters','requesters.company')->orderBy('subject','asc')->paginate(10);
but if you load nested relationship you can use shorter notation - you can omit parent relationship, so it's enough to use here:
Ticket::with('requesters.company')->orderBy('subject','asc')->paginate(10);
Try this, it should work for this case:
Ticket::with('requesters')
->with('requesters.company')
->orderBy('subject','asc')->paginate(10);
I have four tables in database: groups, specialties, lessons, group_lesson. It's structures:
groups
id
specialty_id
name
specialties
id
name
lessons
id
specialty_id
group_lesson (UNIQUE INDEX lesson_id, group_id)
lesson_id
group_id
date
My models look like that for now:
class Group extends Eloquent {
public function specialty() {
return $this->belongsTo('Specialty');
}
}
class Lesson extends Eloquent {
public function specialty() {
return $this->belongsTo('Specialty');
}
}
class Specialty extends Eloquent {
public function lessons() {
return $this->hasMany('Lesson');
}
public function groups() {
return $this->hasMany('Group');
}
}
I need get additional fields in Group model look like that
Group - Eloquent model
name - string
lessons - collection of Lesson models of Group Specialty
date - date from group_lesson table
I've tried different relationships and combinations, but it's doesn't work. Please help me to write correct relationships.
You can use eager-loading to access relational data through relationships, and can even chain relationships further. As a rule of thumb, if you can draw a path to from 1 model to another through a relationship, you can eagerload all the relevant and relational data for that with chained eager-loads.
Laravel Eager Loading
As an example
$speciality_group = Speciality::with('group','lessons')->find($id);
Even though you are only getting a single instance of the speciality model, the related data is hasMany, meaning multiple records. You need to loop through these records using a foreach loop to access the relevant data for them, or alternitavely add additional closures in your initial query to load only a single related model.
foreach($speciality_group->group as $group)
{
echo $group->name;
}
You will need to do this for both instances where you want to display related information.
I need to create relationships between users. As you might expect, they exist in the users table. My approach is a relationships table linking user_a_id to user_b_id.
Then, in the User model, I added:
public function relationships()
{
return $this->belongsToMany('User', 'relationships', 'user_a_id', 'user_id');
}
Unfortunately, I get an error when trying to find the relationships;
dd($user->relationships);
Output: Whoops, looks like something went wrong.
I would consider something like a "user" model, and a "group" model. The users table can have a many-to-many relationship with the groups table, and this could be defined with a "user_group" table, containing a column "user_id" and another "group_id". Then you can have a function in your User model called groups, which does return $this->belongsToMany('Group');, and a function in your Group model called Users, which goes return $this->belongsToMany('User');
Refer to this section of the Eloquent docs for further direction.