2016-10-31 4 views
1

私はcategoryモデル内の関数を作った:私はこの関数を呼び出したコントローラの機能で再帰関数は何も返さdoes notの - laravel

public function getSecondCategoryByID($category_id = 0,$id = 0) 
    { 

     $category = $this->where('id','=',$category_id)->first(); 

     if($category->parent_cat_id == 1) 
     { 

      $mcategory = $this->where('id','=',$id)->first(); 


      echo 'Inside Model '.$mcategory->id; 
      return $mcategory->id; 


     }else{ 
      $this->getSecondCategoryByID($category->parent_cat_id,$category->id); 
     } 

    } 

$category = new Category; 

    $secondCategoryID = $category->getSecondCategoryByID($category_brand->category_id,0); 

    echo 'After Model '.$secondCategoryID ; 
    return ; 

私は私の関数を呼び出すときコントローラ私はこのメッセージをページに持っています:

Inside Model 3After Model //it should be Inside Model 3After Model '3' 

私のモデルはdoesn '何かを返す!なぜならafter modelの後は何もない!!!!!

+3

あなたはELSEブロック – nospor

+0

でRETURNを忘れてしまったあなたは、同じテーブル内の親子再帰的な関係をしようとしていますか? –

+0

@nosporブロックが再帰的プロセスの終わりである場合。 –

答えて

2

再帰を行うときには、elseブロック文を変更して再帰呼び出しも返す必要があります。

else 
{ 
    return $this->getSecondCategoryByID($category->parent_cat_id,$category->id); 
}