Laravel奇怪"试图获得非对象的财产"错误

时间:2015-08-01 03:38:43

标签: php laravel

我正在尝试扩展名为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)。

如果有人知道为什么会这样,请告诉我。

编辑:以下是实际错误: 第20行是$season = $teamSeasonPivot->season; enter image description here

0 个答案:

没有答案