2014-05-15 27 views
0

我試圖創建一個嵌套的childs(使用laravel框架)的uri。Laravel - 嵌套的uri獲得父項的父項(等..)

我的問題是與我目前的代碼,我只檢索上述的孩子的父母。而不是父母高於母體(如果存在的話)等等.....

(我的數據庫表中有一欄「父」與上面的父母在其中的ID)

可有人幫我解決這個問題?

我的代碼:

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

功能我打電話:

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; 
    } 

預先感謝您!

回答

1

你有什麼理由可以使用Request::segement()

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

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

嗯,我檢索URI是單一的,所以他們已經在部分。所以我想將它們「粘合」在一起:P和迄今爲止我只能用http://example.com/parent/child(而不是http://example.com/parent/child/child)< - 現在將返回給我http://example.com/child/child:/ – yinshiro