Something strange is happening with my laravel 4.0 application. I have the table users and the table profiles. In their models:
// app/models/User.php:
public function profile(){
return $this->hasOne('Profile', 'user_id');
}
// app/models/Profile.php:
public function user(){
return $this->belongsTo('User');
}
I am sure all the rows in the table profiles has an user_id and it has a correspondent user in the table users, and vice versa. But when I do this:
$users = User::with('profile')->get();
foreach($users as $user){
if(isset($user->profile)){
print($user->profile->name);
}
else{
print('profile is not set! why?!');
}
}
Sometimes I get the message "profile is not set! Why?!"
So, why?!