【发布时间】:2016-05-14 17:14:43
【问题描述】:
表格:
Posts
id | console_id | game_id | etc
Games
id | name
Console
id | name
现在,当我查询此关系时,我不断收到“尝试获取非对象的属性”错误。现在,如果我将结果限制为前 3 名(这是我在游戏表中所拥有的全部),那么它将运行,但除此之外它会抛出异常......关系错了吗?
关系:
Game
public function Post()
{
return $this->belongsTo('App\Post', 'game_id');
}
Post
public function console()
{
return $this->hasOne('App\Console', 'id');
}
public function games()
{
return $this->hasOne('App\Game', 'id');
}
Console
public function Post()
{
return $this->belongsTo('App\Post', 'console_id');
}
更新
@joel @rashmi 所以实际上倾销了 $post 我在第 4 个条目上看到了这个......它返回 NULL
["relations":protected]=>
array(2) {
["games"]=>
NULL
前 3 个返回值。但随后第 4 次都返回 NULL。同样,我在 Games 表中只有 3 个值
Games Table:
1 | game 1
2 | game 2
3 | game 3
实际上在第三个条目上它的值为 2 但显示游戏 3 名称
posts table:
id | game id
1 | 2
2 | 3
3 | 2 (but showing "game 1" text)
【问题讨论】: