2017-09-26 38 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 
    } 
}