/** * Get a relationship. * * @param string $key * @return mixed */ public function getRelationValue($key) { // If the key already exists in the relationships array, it just means the // relationship has already been loaded, so we'll just return it out of // here because there is no need to query within the relations twice. if ($this->relationLoaded($key)) { // 这个条件会不成立 return $this->relations[$key]; }
// If the "attribute" exists as a method on the model, we will just assume // it is a relationship and will load and return results from the query // and hydrate the relationship's value on the "relationships" array. if (method_exists($this, $key)) { return $this->getRelationshipFromMethod($key); } }
/** * Set the specific relationship in the model. * * @param string $relation * @param mixed $value * @return $this */ public function setRelation($relation, $value) { $this->relations[$relation] = $value;
return $this; }
Laravel 的 Model
有没有这个问题
没有。
Laravel 的 Model 会先去模型的 attributes
里查看是否存在对应的属性,最后才看关联有没有这个关联。