【发布时间】:2016-08-22 00:59:56
【问题描述】:
使用 Laravel 5.2,我在团队模型中设置了一个 mutator:
public function getNameAttribute($value)
{
if ($this->exists) {
return !empty($value) ? $value : $this->organization()->first()->name;
}
return null;
}
与组织模型的关系:
public function organization()
{
return $this->belongsToMany('App\Models\Organization')->withTimestamps();
}
当$team->name 为空时,我尝试获取$this->organization()->first()->name 但是.. 发生错误:
试图获取非对象的属性
两个小时后,我还没有发现问题
编辑:
Schema::table('organization_team', function (Blueprint $table) {
$table->foreign('organization_id')
->references('id')
->on('organizations')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('team_id')
->references('id')
->on('teams')
->onUpdate('cascade')
->onDelete('cascade');
});
【问题讨论】:
-
我找到了另一个话题。我认为我有同样的问题。 link 但我不明白他是如何解决问题的。
标签: laravel orm laravel-5 model