我正在尝试从同一个类中的另一个函数访问函数变量。我对这个概念很新,我可以让它在另一个函数中工作,但是当我尝试创建它自己的函数时,我得到一个Trying to get property of non-object
我知道这意味着什么,但是对于需要返回的内容感到困惑我的功能因为它在我的其他功能中起作用。
获取错误的功能
public function getEditTotal($id) {
$techs = $this->technician();
$tech = $techs->tech;
var_dump($tech); die;
return View::make('report.edit', array('pageTitle' => 'Edit Report Total', 'id' => $id, 'tech' => $tech));
}`
我要调用的功能
public function technician() {
$tech = DB::table('technician')
->get();
return $tech;
}
我在这个函数中有相同的$tech
变量,如果我调用$this->setComplete($id)
,它的效果非常好。
setComplete($ id)函数中的返回语句
return View::make('report.total', array('pageTitle' => 'Reporting', 'id' => $id, 'tech' => $tech, 'status' => $status));
我确信这是它返回的方式,因为该变量在数组中的setComplete($id)
中返回。我只是不知道如何在technician()
函数中严格调用它。
答案 0 :(得分:1)
当您致电$techs = $this->technician();
时,您将$ techs设置为技术人员功能中$ tech变量的值。那将是DB :: table('技师')的结果
- >得到();
理论上,这应该是一个对象数组,其中每个对象代表技术人员表中的一行。
如果你想知道发生了什么,请在你的technician()函数中添加一个var_dump($ tech),就在返回$ tech语句之前。
由于您表明它正在按预期工作,因此您将获得一组对象。我不确定你想对那些做什么,但在控制器里面:
foreach ($techs as $tech) {
echo $tech->somefieldInTech;
}
或者
echo $techs[0]->somefieldInTech;
所以要明确一点,在你的laravel模板中,你可能希望在模板中传递整个$ techs和foreach,尽管从你的代码中你不清楚你需要对数据做什么。