yinshiro
yinshiro

Reputation: 159

Laravel - Nested uri getting parent of parent (etc..)

I'm trying to create a uri with nested childs (using laravel framework).

My problem is with my current code that i only retrieve the above parent of the child. And not the parent above that parent (if it exists) and so on.....

(My database table has a column 'parent' with the id of the above parent in it)

Can someone help me out with this??

My code:

        $pageRoutes = array();
        foreach ($pages as $page) {
            $pageRoutes[] = self::PageRoute($page->id, $page->parent, $page->uri, $searchTerm);
        }

The function i'm calling:

    public function PageRoute($pageId, $parent, $prevUri = null, $searchTerm = false)
    {
        if (isset($parent) && $parent == null)
            $pages = DB::table('tblpages')
                ->select('tblpages.id','tblpages.uri')
                ->whereNull('tblpages.parent')
                ->where('tblpages.is_active', '=', true)
                ->get();
        else
            $pages = DB::table('tblpages')
                ->select('tblpages.id', 'tblpages.uri', 'tblpages.parent')
                ->where('tblpages.parent', '=', $pageId)
                ->where('tblpages.name', 'LIKE', $searchTerm)
                ->where('tblpages.is_active', '=', true)
                ->get();

        $path = array();

        foreach ($pages as $page)
        {
            if ($page->uri != '/')
                $page->uri = $prevUri.'/'.$page->uri;

            $path[$page->id] = $page;

            $path = self::PageRoute($page->id, $page->uri, $prevUri) + $path;
        }

        return $path;
    }

Thank you in advance !!

Upvotes: 0

Views: 882

Answers (1)

Laurence
Laurence

Reputation: 60038

Is there any reason why you can use Request::segement()?

http://example.com/first/second/third

Request::segment(1) // "first"
Request::segment(2) // "second"
Request::segment(3) // "third"

Upvotes: 1

Related Questions