MySQL8.0 の再帰クエリと Eloquent メモ

2022/07/07

with recursive

単独の Eloquent 内で with recursive を使用したい

メソッド確認

unionAll

外部パッケージ

もはや生PDOに回帰するか

whereIn()


最終的には

$hogeObjectsArray = $this->dbConnect->connection('hogera')
                                ->select("
                                    WITH recursive child(
                                        depth,
                                        id,
                                        parent,
                                        name
                                      ) AS (
                                        SELECT 0,
                                               id,
                                               parent,
                                               name
                                          FROM hoge
                                         WHERE id = {$foo}
                                         UNION ALL
                                        SELECT child.depth + 1,
                                               hoge.id,
                                               hoge.parent,
                                               hoge.name
                                          FROM hoge, child
                                         WHERE hoge.parent = child.id
                                      )
                                      SELECT depth,
                                             id,
                                             parent,
                                             name
                                        FROM child
                                       ORDER BY
                                             depth
                                ");

の形で取得てきた。 ->table($this->table) 等でテーブル指定せずにいきなり ->select() しているのがミソ。


Written by Circle
A mound built by the accumulation of hyperlinks are like Kowloon.