Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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 :

Table layout

I need to display the fldUserFullName of the id stored in the fldUserParentID column.

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0
Solution

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

Thanks for your help I have eventually got this working using your advice above.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mharrisweb mharrisweb Joined 23 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.