2017-08-17 68 views
0

我有錯誤是這樣的:LAravel作曲家查看相關性不看

Unresolvable dependency resolving [Parameter #0 [ <required> $products ]] in class App\Http\ViewComposers\ShoppingCartComposer 

我想從CartController送我的數據ViewComposer(這裏$產品),但我的依賴並不這麼看。

我的代碼是: 的View Composer:

namespace App\Http\ViewComposers; 

use Illuminate\View\View; 

class ShoppingCartComposer 
{ 

protected $products; 


public function __construct(Array $products) 
{ 
    // Dependencies automatically resolved by service container... 
    $this->products = $products; 
} 

public function countProducts(){ 

} 

public function compose(View $view) 
{ 
    $view->with('count', 'test'); 
} 
} 

這裏是作曲服務提供商:

namespace App\Providers; 

use Illuminate\Support\Facades\View; 
use Illuminate\Support\ServiceProvider; 

class ComposerServiceProvider extends ServiceProvider 
{ 

public function boot() 
{ 

    View::composer(
     'shopping-cart', 'App\Http\ViewComposers\ShoppingCartComposer' 
    ); 

} 

public function register() 
{ 

} 

}

這裏是購物車Conrtoller從那裏我溫妮派&產品從索引控制器:

class CartController extends Controller 
{ 
public function index() 
{ 
    if (Session::has('shoppingCart')) { 
     $products = Session::get('shoppingCart'); 
    } else { 
     $products = []; 
    } 

    return view('shopping-cart', ['products' => $products]); 
} 
+0

試試這個鏈接https://scotch.io/amp/tutorials/sharing-data-between-views-using-laravel-view-composers – Dhaval

+0

我wriote這個,但仍然不解決我的問題:( – wenus

回答

0

它沒有找到變量$products因爲你需要將它傳遞到撰寫方法在您看來作曲:

public function compose(View $view) 
{ 
    $view->with([ 
     "products" => $this->products, 
     "test" => "TEST" 
    ]); 
} 

希望這有助於你。