Firstly, are Users and UserParents to different tables in the DB? Or are they in the same table with different roles?
There is also a convention that is wise to follow:
// User table
id | parent_id | ...
// Parents table
id | ...
Note that the user (child) references the parent_id. Doing this way makes it very easy to read and understand. I'll use this convention later rather than yours if that is OK.
What you need to do is create the relationship on both sides:
class User extends Eloquent {
public function parent(){
// Model namespace | foreign key | local key
return $this->hasOne('UserParent', 'id', 'parent_id');
}
}
// I never remember, or get the foreign key and local key the right way round. This might be wrong, but hopefully you get the idea.
class Parent extends Eloquent {
public function children(){
return $this->belongsToMany('User');
}
}
Another note is that you are using the 'get' method - Which is an Accessor - Basically, when you 'get' say 'first_name' you want to run a function over it - like making it uppercase. Basically, you don't need the 'get' to make the relationship.
Once you have defined this relationship, you can get all parents like this in your Controller:
$user = User:find(1);
$user_parents = $user->parents; // See here you are referencing the 'parent' method that is on your User model.
// The reverse of this - get children of a parent:
$parent = Parent::find(1);
$children = $parent->children;
Hope that helps.
Thanks very much for your reply.
Yes it uses the same table, both ID's are in the same table.
I am trying to loop through the users and display the parent for each user in the loop. Something like this if its possible?
@foreach ($users as $user)
{{ $user->fldUserFullName}} -- Outputs UserFullName OK
{{ $user->getUserParent->fldUserFullName; }} -- Trying to output parents full name
@endforeach
I have uploaded a screenshot here, fldUserID is the PK, fldUserParentID is the parentid :
I need to display the fldUserFullName of the id stored in the fldUserParentID column.
Thats fine. However you structure the DB is OK. However, note the 'getUserParent' is wrong and is acting as a Accessor.
You need to define the relationships properly as mentioned in my first reply. Have a bash at that and you should be sorted.
Thanks very much again for your reply.
I still get the error : trying to get property of non-object
My user model:
public function parent(){
return $this->hasOne('UserParent', 'fldUserID', 'fldUserParentId');
}
My UserParent Model:
public function pupil(){
return $this->belongsToMany('User');
}
That should work shouldn't it?
What is your query looking like?
Should be something like:
$pupil = Pupil::find(1);
$parent = $pupil->parent // Ideally you may want to rename this 'parent' on your model and here to 'parents' as I presume you may want to have more than one parent - Its only a slight change but makes more grammatical and programmatic sense.
As mentioned, I generally mess up the relationship set up and have to think about it to get it sorted. This WILL work, you will just need to make sure you relationships are set up in the right way.
http://laravel.com/docs/4.2/eloquent#relationships
You are on the way. Just make sure the relationships are set up properly. One thing to note is that as you have non-standard naming for your columns, you'll need to set the FK and LK on both sides of the relationship properly.
If you have a look at the docs and make yourself familiar with them, if I have made an error in the example, you should be able to spot it.
Thanks very much for your reply again, really appreciate it.
I'm getting there, I'm display the parent data but its not quite right. Its a has-one relationship I need just for now. Each user can only have one parent, but one parent can have many users. Does that make sense?
public function parent(){
return $this->hasOne('UserParent', 'fldUserParentId');
}
I am returning all users and using the relationship to show the parent if they are a user (pupil):
public function getUsers(){
$user = User::paginate(15);
return View::make('layouts.users',
array('users' => $user));
}
At the moment I'm displaying this data:
User 1 UserID = 46 ParentID = 75
User 2 UserID = 75 ParentID = 0
Using this loop:
@foreach ($users as $user)
{{ $user->fldUserID;}}
@if ($user->parent)
{{ $user->parent->fldUserID; }}
@endif
@endforeach
I am getting the $user->fldUserID, but the $user->parent->fldUserID is not displaying anything, when it should display 75.
Do you have any ideas why its not returning anything?
Many thanks again
No worries. Glad I Can help.
Does the parent have fldUserId? I presume it does. What happens with this?
@foreach ($users as $user)
{{ $user->fldUserID;}}
{{ $user->parent[0]->fldUserID }} // I'll explain this
{{ var_dump($user->parent) }}. // this will dump out the value of parent if there is one.
@if ($user->parent)
{{ $user->parent->fldUserID; }}
@endif
@endforeach
As a child can have MANY users, Laravel will return an array. So you'll need to foreach through the parents in the same way you do for the child.
That should work... I hope. The var_dump() will show you the object that is returned for each child.
Thanks T2theC,
I'm getting results now, I only seem to able to get the parentID of users that appear before the current row.
A user with UserID 5, if I set the parentID to 50 for example. Nothing shows, but if I set the parentID to 3, it shows fine.
Is there a way around that? Is it to do with Eager/ Lazy loading?
Eager loading is a way to reduce The queries that are being made. It's is definitely worth adding. It's pretty easy. I haven't menioned it so we don't confuse things. The results should be the same, but you are saving the number of quries.
As you are not using the standard primary key name of 'id', you might be getting lost there. On both your models, have you indicated what the primary key is? You don't need to if it is, id, you need to define it so the relationship knows what is what.
protected $primaryKey = 'name of pk';
That might fix it for you.
Thanks for your help I have eventually got this working using your advice above.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community