【问题标题】:Laravel parent / child relationship on the same model同一模型上的 Laravel 父/子关系
【发布时间】:2015-03-05 04:29:32
【问题描述】:

设置和虚拟数据

我有一个名为 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 的输出是什么?

标签: php laravel


【解决方案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's value on the "relationships" array.
    $camelKey = camel_case($key);

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

然后在getAttribute 首先是一些检查“正常”属性然后返回的代码。最后,在方法的最后,如果有定义的关系方法getRelationshipFromMethod 会被调用。

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

【讨论】:

  • 好的,所以在这种情况下,孩子调用上的括号不是必需的。在一个非常漂亮的框架中有点令人困惑,但这解决了这个问题。谢谢。
  • 不是真的..我用解释更新了我的答案:)
  • 完美!谢谢你非常详细的解释。
【解决方案2】:

在模型中设置并尝试:

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

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

【讨论】:

  • Affarin, eyval ;)
  • khahesh :) @DolDurma
  • Adam eftekhar mikone 在 javanan irani :)
  • @MostafaNorzade mamnoonam :)
猜你喜欢
  • 1970-01-01
  • 2016-04-17
  • 2015-10-13
  • 2019-07-30
  • 2016-07-12
  • 2020-12-19
  • 2018-01-28
相关资源
最近更新 更多