我有一个名为Category的简单模型,它具有以下模式:
|----------------------------------------------|
| cat_id | cat_name | parent_id |
|----------------------------------------------|
| 1 | Home | 0 |
|----------------------------------------------|
| 2 | Products | 1 |
|----------------------------------------------|
| 3 | Services | 1 |
|----------------------------------------------|
| 4 | Product A | 2 |
|----------------------------------------------|
| 5 | Product B | 2 |
|----------------------------------------------|
Run Code Online (Sandbox Code Playgroud)
所以你可以看到我们将得到一个非常直接的层次结构,如下所示:
Home
- Products
- Product A
- Product B
- Services
Run Code Online (Sandbox Code Playgroud)
我正在尝试在Laravel 4.2中映射这种关系,以便我可以查询模型并获取其父项(它将始终具有父项)和子项类别(如果它们存在).
我使用以下方法在Category模型中定义了关系:
public function children()
{
return $this->hasMany('Category', 'parent_id', 'cat_id');
}
public function parent()
{
return $this->belongsTo('Category', 'parent_id');
}
Run Code Online (Sandbox Code Playgroud)
我可以使用,让父名称正常工作
$category = Category::findOrFail($id);
return $category->parent->cat_name;
Run Code Online (Sandbox Code Playgroud)
但是,我不明白如何获取子对象.
我试过了:
$category = Category::findOrFail($id);
$children = $category->children();
Run Code Online (Sandbox Code Playgroud)
但是当我dd($ children)它没有输出我所期望的.
luk*_*ter 28
调用关系函数(->children())将返回关系类的实例.您需要调用then get()或只使用该属性:
$children = $category->children()->get();
// or
$children = $category->children;
Run Code Online (Sandbox Code Playgroud)
实际上children()并且children是非常不同的东西.children()只需调用您为关系定义的方法.该方法返回一个对象HasMany.您可以使用它来应用更多查询方法.例如:
$category->children()->orderBy('firstname')->get();
Run Code Online (Sandbox Code Playgroud)
现在访问该属性的 children方式不同.你从未定义它,所以Laravel在后台做了一些魔术.
我们来看看Illuminate\Database\Eloquent\Model:
public function __get($key)
{
return $this->getAttribute($key);
}
Run Code Online (Sandbox Code Playgroud)
__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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后getAttribute首先是一些检查"正常"属性然后返回的代码.最后,在方法结束时,如果定义了关系方法getRelationshipFromMethod被调用.
然后它将检索关系的结果并返回该结果.
在模型中设置它并尝试:
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public function grandchildren()
{
return $this->children()->with('grandchildren');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25336 次 |
| 最近记录: |