为什么 laravel 急切加载约束不起作用?
why is laravel eager loading constraint not working?
我将此作为我想做的事情的基础,但约束不适用
$this['subs'] = Cat::with([
'children' => function($query) {
$query -> where('parent_id', 0);
}
]) -> get();
你要找的是 whereHas
,像这样:
$this['subs'] = Cat::whereHas('children', function ($query) {
$query->where('parent_id',0);
})->get();
将查询放在 with
中只是限定它查询的关系的范围,而不是记录集 cat
本身。
你有 id = 0 的猫吗?
尝试:
Cat::with('children')->find(0)
假设你有一只 id = 0 的猫;这将 return id = 0 的猫及其 children 如果有的话。
或者:
Cat::has('children')->get()
这将 return 所有具有 children 的猫。
或者:
Cat::with('children')->get()
这将 return 所有猫及其 children 如果有
您正在使用 October CMS
所以您可以只使用 traits nested-tree
文档:https://octobercms.com/docs/database/traits#nested-tree
class Cat extends Model
{
use \October\Rain\Database\Traits\NestedTree;
}
现在
$model = Cat::where('slug', $param_slug)->first();
然后是
$model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded.
// OR
$model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.
你可以ton of options
https://github.com/octobercms/library/blob/master/src/Database/Traits/NestedTree.php
/* General access methods:
*
* $model->getRoot(); // Returns the highest parent of a node.
* $model->getRootList(); // Returns an indented array of key and value columns from root.
* $model->getParent(); // The direct parent node.
* $model->getParents(); // Returns all parents up the tree.
* $model->getParentsAndSelf(); // Returns all parents up the tree and self.
* $model->getChildren(); // Set of all direct child nodes.
* $model->getSiblings(); // Return all siblings (parent's children).
* $model->getSiblingsAndSelf(); // Return all siblings and self.
* $model->getLeaves(); // Returns all final nodes without children.
* $model->getDepth(); // Returns the depth of a current node.
* $model->getChildCount(); // Returns number of all children.
*
* Query builder methods:
*
* $query->withoutNode(); // Filters a specific node from the results.
* $query->withoutSelf(); // Filters current node from the results.
* $query->withoutRoot(); // Filters root from the results.
* $query->children(); // Filters as direct children down the tree.
* $query->allChildren(); // Filters as all children down the tree.
* $query->parent(); // Filters as direct parent up the tree.
* $query->parents(); // Filters as all parents up the tree.
* $query->siblings(); // Filters as all siblings (parent's children).
* $query->leaves(); // Filters as all final nodes without children.
* $query->getNested(); // Returns an eager loaded collection of results.
* $query->listsNested(); // Returns an indented array of key and value columns.
*
* Flat result access methods:
*
* $model->getAll(); // Returns everything in correct order.
* $model->getAllRoot(); // Returns all root nodes.
* $model->getAllChildren(); // Returns all children down the tree.
* $model->getAllChildrenAndSelf(); // Returns all children and self.
*
* Eager loaded access methods:
*
* $model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded.
* $model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.
*
*/
亲子关系真的很方便快捷,请大家看看
如有疑问请评论。
我将此作为我想做的事情的基础,但约束不适用
$this['subs'] = Cat::with([
'children' => function($query) {
$query -> where('parent_id', 0);
}
]) -> get();
你要找的是 whereHas
,像这样:
$this['subs'] = Cat::whereHas('children', function ($query) {
$query->where('parent_id',0);
})->get();
将查询放在 with
中只是限定它查询的关系的范围,而不是记录集 cat
本身。
你有 id = 0 的猫吗?
尝试:
Cat::with('children')->find(0)
假设你有一只 id = 0 的猫;这将 return id = 0 的猫及其 children 如果有的话。
或者:
Cat::has('children')->get()
这将 return 所有具有 children 的猫。
或者:
Cat::with('children')->get()
这将 return 所有猫及其 children 如果有
您正在使用 October CMS
所以您可以只使用 traits nested-tree
文档:https://octobercms.com/docs/database/traits#nested-tree
class Cat extends Model
{
use \October\Rain\Database\Traits\NestedTree;
}
现在
$model = Cat::where('slug', $param_slug)->first();
然后是
$model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded.
// OR
$model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.
你可以ton of options
https://github.com/octobercms/library/blob/master/src/Database/Traits/NestedTree.php
/* General access methods:
*
* $model->getRoot(); // Returns the highest parent of a node.
* $model->getRootList(); // Returns an indented array of key and value columns from root.
* $model->getParent(); // The direct parent node.
* $model->getParents(); // Returns all parents up the tree.
* $model->getParentsAndSelf(); // Returns all parents up the tree and self.
* $model->getChildren(); // Set of all direct child nodes.
* $model->getSiblings(); // Return all siblings (parent's children).
* $model->getSiblingsAndSelf(); // Return all siblings and self.
* $model->getLeaves(); // Returns all final nodes without children.
* $model->getDepth(); // Returns the depth of a current node.
* $model->getChildCount(); // Returns number of all children.
*
* Query builder methods:
*
* $query->withoutNode(); // Filters a specific node from the results.
* $query->withoutSelf(); // Filters current node from the results.
* $query->withoutRoot(); // Filters root from the results.
* $query->children(); // Filters as direct children down the tree.
* $query->allChildren(); // Filters as all children down the tree.
* $query->parent(); // Filters as direct parent up the tree.
* $query->parents(); // Filters as all parents up the tree.
* $query->siblings(); // Filters as all siblings (parent's children).
* $query->leaves(); // Filters as all final nodes without children.
* $query->getNested(); // Returns an eager loaded collection of results.
* $query->listsNested(); // Returns an indented array of key and value columns.
*
* Flat result access methods:
*
* $model->getAll(); // Returns everything in correct order.
* $model->getAllRoot(); // Returns all root nodes.
* $model->getAllChildren(); // Returns all children down the tree.
* $model->getAllChildrenAndSelf(); // Returns all children and self.
*
* Eager loaded access methods:
*
* $model->getEagerRoot(); // Returns a list of all root nodes, with ->children eager loaded.
* $model->getEagerChildren(); // Returns direct child nodes, with ->children eager loaded.
*
*/
亲子关系真的很方便快捷,请大家看看
如有疑问请评论。