Почему Laravel не работает ограничение загрузки? - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть основа того, что я хочу сделать, но ограничение не применяется

$this['subs'] = Cat::with([
    'children' => function($query) {
        $query -> where('parent_id', 0);
    }
]) -> get();

Ответы [ 3 ]

0 голосов
/ 27 февраля 2019

У вас есть кошка с id = 0?

Попробуйте:

Cat::with('children')->find(0)

Предположим, у вас есть кошка с id = 0;это вернет кота с id = 0 и его потомков, если он есть.

Или:

Cat::has('children')->get()

Это вернет всех кошек, которые имеют детей.

Или:

Cat::with('children')->get()

Это вернет всех кошек и их детей, если они есть

0 голосов
/ 27 февраля 2019

вы используете 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.
 *
 */

пожалуйста, проверьте его действительно быстрый и удобный способ для родителейи отношения с детьми.

если есть сомнения, пожалуйста, прокомментируйте.

0 голосов
/ 27 февраля 2019

тот, который вы ищете, это whereHas, вот так:


$this['subs'] = Cat::whereHas('children', function ($query) {
                  $query->where('parent_id',0);
               })->get();

, помещая запрос в with, просто ограничивает отношения, которые он запрашивает, а не сам набор записей cat.

...