2017-09-26 37 views
0

在laravel我想調用函數內的功能,使recursive.I內的另一個功能抓住了路線error.how叫「在tableFetch遞歸」調用的函數

class queryTest extends Controller 
{ 
public function tableFetch() { 
    recursive(); 
} 
function recursive(){ 
    //condition 
} 
} 

我想要的功能這樣做是爲了檢查給定人員的經理,然後在查詢中獲取提取值的經理,因此需要遞歸執行。

+0

這不是一個好主意,打電話內的另一個控制器的方法,相反,你可以使用重定向。你能解釋一下爲什麼你需要這樣做嗎? –

+2

你可以使用'$ this-> recursive()'調用同一個類的函數 –

回答

1

控制器不是一個適合此的好地方。相反,在你的人員模型(或任何你有的)中進行管理。

每個人都有一個經理。所以,你的模型與HasOne有關係。

角色模型:

public function manager() 
{ 
    return $this->hasOne(Person::class, 'manager_id'); 
} 

現在,如果你需要檢查特定的人,你滿足一定的條件,你可以做到這一點的模型內,得到的結果在控制器,直到經理。

public function checkManager() 
{ 
    $manager = $this->manager 

    if (check manager) 
     return $manager; 

    //check for the last manager 
    return $this->manager ? $this->checkManager() : null; 
} 

內部控制

function index() 
{ 
    $person = Person::find($id); 

    $manager = $person->checkManager();// this will do the recursive you need 
} 
+0

什麼是$ person = Person :: find($ id);返回 – user3386779

+0

您想檢查他的經理的第一個人。 –

0

你要問更精確的細節您的需求,因爲Laravel有一定的併發症。 嘗試這樣做:

class queryTest extends Controller 
{ 
    public function tableFetch() { 
     $this->recursive(); 
    } 
    public function recursive() { 
     //condition 
    } 
} 
1

做這樣的事情

class queryTest extends Controller 
{ 
    public function tableFetch() { 
    $this->recursive(); 
} 
    function recursive(){ 
    //condition 
    } 
}