Reputation: 1644
Im running the latest version of Laravel. I am trying to set up relationships with tables but I seem to always get some sort of error. The data that I want to be displayed is displayed but then under everything there is a big error saying:
ErrorException in routes.php line 26: Trying to get property of non-object
In my routes.php file this is the code:
Route::get('select', function() {
$users = App\UserInfo::all();
foreach ($users as $u) {
echo $u->userID.' USERNAME: '.$u->userData->username.'<br/>';
echo $u->userOption->dis_joindate.'<br/>'; // Line 26
}
});
Line 25 seems to work fine printing no errors, though the next line gives the object error.
This is my UserInfo Model php file:
And this is my Options Model php file:
All that my database supplies (in the useroptions table) is and id, userID, dis_joindate, dis_location, dis_posts and dis_likes. And the last 4 colums just give text.
This is the image of the output on the page when all this is ran:
Upvotes: 0
Views: 1804
Reputation: 78
you have a problem with relaitions in userOptions, it should have at least one record for every user. in this case user 27 have no recorde in table userOptions and it its value is null and you try to use a property of null object
Upvotes: 0
Reputation: 111829
The problem here (assuming relationship are defined well) might be data. You might not have related data for each object.
So instead of:
foreach ($users as $u) {
echo $u->userID.' USERNAME: '.$u->userData->username.'<br/>';
echo $u->userOption->dis_joindate.'<br/>'; // Line 26
}
you should rather use:
foreach ($users as $u) {
echo $u->userID.' USERNAME: '.(($u->userData) ? $u->userData->username : '-').'<br/>';
echo (($u->userOption) ? $u->userOption->dis_joindate : '-').'<br/>'; // Line 26
}
In addition to lower number of queries instead of:
$users = App\UserInfo::all();
you should use eager loading like so:
$users = App\UserInfo::with('userData', 'userOption')->get();
Upvotes: 1
Reputation: 62228
Your userOption
relationship isn't setup quite right. Because of this, the relationship will (unexpectedly to you) return null
when the related record isn't found, and you'll get this error when you attempt to access dis_joindate
on the null
object.
In the comments you mention that you do have a useroptions
record with a userID
of 27, however that is not the record that is being loaded. The way your relationship is setup, it is actually looking for a useroptions
record with an id
of 27 (not userID
).
When defining a belongsTo
relationship, the second parameter is the name of the foreign key field on the Model which has the belongsTo
relationship. In this case, when you specify userID
, you're specifying userinfo.userID
. The third parameter is the name of the field on the related Model on which to join. If not specified, it defaults to the primary key (usually id
). Since you have not specified this parameter, it will default to useroptions.id
.
If you would like relate records where userinfo.userID = useroptions.userID
, you need to add the name of the related field to your relationship:
public function userOption() {
return $this->belongsTo('\App\Options', 'userID', 'userID');
}
You didn't mention the structure of the userData table, but you may want to check that out, as well.
Upvotes: 1