Laravel 4: Good relations get all the data

I have 2 relationship data tables; table users and memberdetails.

Users.php

class Users extends Eloquent{ public function memberdetails() { return $this->hasOne('Memberdetails','user_id'); } } 

Memberdetails.php

 class Memberdetails extends Eloquent{ public function user() { return $this->belongsTo('Users','user_id'); } } 

When I try to get data, $data = User::find($id); I only get data from the users table.

An example of the shape of my blade:

 {{-- User Name, stored on user table --}} {{ Form::text('name',null, array('id'=>'name','class'=>'form-control','required')) }} {{-- User address, stored on member table --}} {{ Form::text('address',null, array('id'=>'address','class'=>'form-control','required')) }} 

When I am, localhost/user/2/edit/ , the name field is populated, but the address field is empty. How can I get data from both tables and add to the form for editing?

Thanks.

+6
source share
2 answers

You can use active download.

 $user = User::with('memberdetails')->find($id); 

Using this, you will automatically get item details when you retrieve the user. Then you can use $user->memberdetails

Using impatient loading, you only make one query in the database, so it should be preferred. If you are not using with('memberdetails') , you will execute the second request when accessing element elements.

+22
source

After receiving the user instance, open the link, then you can access the other properties of the class

 $user = User::find($id); $userData = $user->memberdetails; 
0
source

All Articles