我正在尝试扩展名为eBot的应用程序,该应用程序最初是使用Symfony 1.4制作的。我正在使用Laravel 5.1。
我要做的是返回一组"团队"以及以下格式的季节:
{TeamName}<small>{SeasonName}</small>
这是我目前的代码:
$teams = [];
foreach(eBotTeam::all() as $team)
{
$teamSeasonPivot = eBotTeamSeasonPivot::where('team_id', '=', $team->id)->get()->first();
$season = $teamSeasonPivot->season;
$teams[$team->id] = $team->name . '<small>' . $season->name . '</small>';
}
我的模型如下所示:
class eBotSeason extends Model
{
protected $table = 'seasons';
protected $connection = 'ebot';
protected $fillable = ['name', 'event', 'start', 'end', 'link', 'logo', 'active'];
public function matches()
{
return $this->hasMany('\Doesplay\Models\eBotMatch', 'season_id');
}
}
class eBotTeam extends Model
{
protected $table = 'teams';
protected $connection = 'ebot';
protected $fillable = ['name', 'shorthandle', 'flag', 'link', 'dp_team_id'];
}
class eBotTeamSeasonPivot extends Model
{
protected $table = 'teams_in_seasons';
protected $connection = 'ebot';
protected $fillable = ['season_id', 'team_id'];
public function team()
{
return $this->belongsTo('Doesplay\Models\eBotTeam');
}
public function season()
{
return $this->belongsTo('Doesplay\Models\eBotSeason');
}
}
当我死并转储$teamSeasonPivot
或$season
时,它可以正常工作并显示应该的模型。但是,如果我尝试将其放入数组中,如上所示,它会在Trying to get property of non-object
上返回$season = $teamSeasonPivot->season;
错误。
在错误转储中,它显示数组($teams
)。
如果有人知道为什么会这样,请告诉我。