Ответ 1
Вызов функции отношений (->children()
) возвращает экземпляр класса отношений. Вам нужно либо позвонить, либо get()
, либо просто использовать свойство:
$children = $category->children()->get();
// or
$children = $category->children;
Дальнейшее объяснение
На самом деле children()
и children
- это нечто совсем другое. children()
просто вызывает метод, который вы определили для своих отношений. Метод возвращает объект HasMany
. Вы можете использовать это, чтобы применить дополнительные методы запросов. Например:
$category->children()->orderBy('firstname')->get();
Теперь доступ к свойству children
работает по-разному. Вы никогда не определяли его, поэтому Laravel делает магию в фоновом режиме.
Посмотрим на Illuminate\Database\Eloquent\Model
:
public function __get($key)
{
return $this->getAttribute($key);
}
Функция __get
вызывается при попытке получить доступ к свойству объекта PHP, который фактически не существует.
public function getAttribute($key)
{
$inAttributes = array_key_exists($key, $this->attributes);
// If the key references an attribute, we can just go ahead and return the
// plain attribute value from the model. This allows every attribute to
// be dynamically accessed through the _get method without accessors.
if ($inAttributes || $this->hasGetMutator($key))
{
return $this->getAttributeValue($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 (array_key_exists($key, $this->relations))
{
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 value on the "relationships" array.
$camelKey = camel_case($key);
if (method_exists($this, $camelKey))
{
return $this->getRelationshipFromMethod($key, $camelKey);
}
}
Затем в getAttribute
сначала находится некоторый код, который проверяет "нормальные" атрибуты и возвращает. И, наконец, в конце метода, если существует метод отношения, определенный getRelationshipFromMethod
.
Затем он получит результат отношения и вернет его.