Laravel Get ancestors (URL)(Laravel 获取祖先(URL))
问题描述
在 Laravel 中,我有一个包含 id、parent_id、slug (Self-referring) 的表,
In Laravel, I have a table which contains id, parent_id, slug (Self-referring),
当我有一个ID时,我需要以这样的格式(以/"分隔)获取其所有祖先.
When I have an ID, I need to get all its ancestors in a format like this (Separated by "/").
level1/level2/level3
但是在没有像laravel-nestedset"这样的包的情况下以一种有效的方式".
But in an efficient way without a package like "laravel-nestedset ".
我是这样实现的.
public function parent()
{
return $this->belongsTo('Collection', 'parent_id');
}
public function getParentsAttribute()
{
$parents = collect([]);
$parent = $this->parent;
while(!is_null($parent)) {
$parents->push($parent);
$parent = $parent->parent;
}
return $parents;
}
还有其他方法可以有效地做到这一点并用/"分隔吗?
Any other way to do it efficiently and separated by "/" ?
推荐答案
在评论中进行了一些交谈后,我认为这是一个很好的解决方案:
After a little conversation in the comments I think this is a good solution:
// YourModel.php
// Add this line of you want the "parents" property to be populated all the time.
protected $appends = ['parents'];
public function getParentsAttribute()
{
$collection = collect([]);
$parent = $this->parent;
while($parent) {
$collection->push($parent);
$parent = $parent->parent;
}
return $collection;
}
然后您可以使用以下方法检索您的父母:
Then you can retrieve your parents using:
YourModel::find(123)->parents
(集合实例)YourModel::find(123)->parents->implode('yourprop', '/')
(内爆为字符串,见 https://laravel.com/docs/5.4/collections#method-implode)YourModel::find(123)->parents->reverse()->implode('yourprop', '/')
(倒序https://laravel.com/docs/5.4/collections#method-reverse)
YourModel::find(123)->parents
(collection instance)YourModel::find(123)->parents->implode('yourprop', '/')
(imploded to string, see https://laravel.com/docs/5.4/collections#method-implode)YourModel::find(123)->parents->reverse()->implode('yourprop', '/')
(reversed order https://laravel.com/docs/5.4/collections#method-reverse)
正如 Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 所指出的,您也可以将它组合起来这样可以节省一些查询:
As noted by Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 you may also combine it with this to save a few queries:
protected $with = ['parent.parent.parent'];
// or inline:
YourModel::find(123)->with(['parent.parent.parent']);
这会在对象加载时预加载父级.如果您决定不使用它,则在您调用 $yourModel->parent
时(延迟)加载父项.
This preloads the parent on object load. If you decide not to use this, the parent is (lazy) loaded as soon as you call $yourModel->parent
.
这篇关于Laravel 获取祖先(URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 获取祖先(URL)
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01