Laravel同一模型的父子关系

26

设置和虚拟数据

我有一个简单的模型叫做分类(Category),它有以下架构:

|----------------------------------------------|
| cat_id   | cat_name    | parent_id           |
|----------------------------------------------|
|   1      | Home        |   0                 |
|----------------------------------------------|
|   2      | Products    |   1                 |
|----------------------------------------------| 
|   3      | Services    |   1                 |
|----------------------------------------------|
|   4      | Product A   |   2                 |
|----------------------------------------------|
|   5      | Product B   |   2                 |
|----------------------------------------------|

期望输出

因此,您可以看到我们将获得一个非常简单明了的层次结构,如下所示:

Home
  - Products
      - Product A
      - Product B
  - Services

问题

我正在尝试在 Laravel 4.2 中映射此关系,以便可以查询模型并获取其父项(它始终会有一个父项),以及如果存在,则获取子类别。

我已经在 Category 模型中定义了这个关系,使用:

public function children()
{
    return $this->hasMany('Category', 'parent_id', 'cat_id');
}
public function parent()
{
    return $this->belongsTo('Category', 'parent_id');
}

问题

我可以使用以下方法获取父级名称:

$category = Category::findOrFail($id);
return $category->parent->cat_name;

然而,我不明白如何获取子对象。

我尝试过:

$category = Category::findOrFail($id);
$children = $category->children();

但是当我使用dd($children)时,输出的结果与我预期的不符。


$children 的输出是什么? - DouglasDC3
2个回答

41
调用关系函数(->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's value on the "relationships" array.
    $camelKey = camel_case($key);

    if (method_exists($this, $camelKey))
    {
        return $this->getRelationshipFromMethod($key, $camelKey);
    }
}

getAttribute方法中首先检查“正常”的属性并返回它们。最后,如果定义了关系方法,则调用getRelationshipFromMethod方法。

然后它将检索关系的结果并返回。


好的,所以在这种情况下,孩子调用中的括号是不必要的。虽然在一个非常美丽的框架中有点令人困惑,但这解决了问题。谢谢。 - Amo

5

将其设置在模型中并尝试:

public function children()
{
    return $this->hasMany(self::class, 'parent_id');
}

public function grandchildren()
{
    return $this->children()->with('grandchildren');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接