2017-07-18 45 views
0

在我的Laravel 5.4項目中,我有一個Helpers.php文件。這很好。Php幫助函數參數undefined

現在,我做了一個幫手,看起來像這樣:

if (! function_exists('issetWithReturn')) { 
    /** 
    * @return mixed 
    */ 
    function issetWithReturn($values) 
    { 
     return isset($collection) ? $collection : ''; 
    } 
} 

在我OrganisationController.php我用這樣的:

/** 
* Show all organisations. 
* 
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 
*/ 
public function index() 
{ 
    if (Gate::allows('edit-organisations')) { 
     $products = $this->productRepo->getAll(); 
    } 

    return view('organisation.index')->with([ 
     'products' => issetWithReturn($products), 
    ]); 
} 

但我的編輯已經給出了一個跡象,表明$productsissetWithReturn未定義?這是爲什麼?

當我嘗試這樣它的所有工作:

'products' => isset($products) ? $products : '', 
+1

您是否包含幫助文件? –

+0

是的,我使用更多的輔助功能,他們正常工作。它在我的'''composer.json'''中。我跑了'''作曲家dumpautoload -o'''。但這不是問題。 – Jenssen

回答

1

那麼你傳遞一個參數調用$values但使用一個名爲varible在函數內部$collection

所以基本上它是一個錯字

if (! function_exists('issetWithReturn')) { 
    /** 
    * @return mixed 
    */ 
    function issetWithReturn($values) 
    { 
     return isset($value) ? $value : ''; 
    } 
}