2017-09-26 86 views
0

我有laravel 5.3項目 我嘗試了所有的意見分享份額可變.. 於是我就在appserviceproviders 嘗試這一.. 這是開機功能有意見模型

View::share('path', '/final/public/'); 

確定其工作.. ,但我想做的事是 我的意思是這樣像數模式記錄所有視圖共享變量..

$items = Item::get(); 
View::share('variable',$items); 

但其無不剪任何東西。 所以我試圖把它放在這個類..

class Controller extends BaseController 
{ 
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; 
private $itemss; 
public function __construct() 
{ 
    $this->middleware(function ($request, $next) { 
     $this->itemss = Item::get(); 
     view()->share('itemss', $this->itemss); 
    }); 

} 
} 

,我騎上了同樣的錯誤

ErrorException in 4f8648bebb04d05a1427fdfec486dd0221e1b875.php line 235: 
Undefined variable: itemss (View: 
E:\AppServ\www\final\resources\views\layouts\adminmaster.blade.php) (View: 
E:\AppServ\www\final\resources\views\layouts\adminmaster.blade.php) 
+1

可能需要變量的範圍與使用擴展()關閉。 –

+0

你能不能代碼 –

回答

1

您可能需要變量的範圍與use關鍵字擴展。

public function __construct() 
{ 
    $this->middleware(function ($request, $next) use ($itemss) { 
     $this->itemss = Item::get(); 
     view()->share('itemss', $this->itemss); 
    }); 
} 

從文檔:

的封閉封裝其範圍,這意味着它具有在其中它被定義或執行 範圍沒有訪問。然而,有可能從父範圍(如封閉的定義) 繼承的變量 與使用關鍵字

+0

private $ itemss; \t公共職能__construct() \t { \t \t $這個 - >中間件(函數()使用($ itemss){ \t \t \t $這個 - > itemss =項目::得到(); \t \t \t視圖() - > share('itemss',$ this-> itemss); \t \t}); \t}不工作 –