2016-09-16 104 views
0

我试图在网络中传递一些值,所以我创建了View Composer,但返回'Undefined variable'。我在商View Composer Laravel 5.3

use Illuminate\Support\Facades\View; 
    public function boot() 
    { 
    View::composer(['front.index'],'App\Http\ViewComposers\AsideComposer'); 
    } 

添加以下代码在AsideComposer我添加

use Illuminate\Contracts\View\View; 
use App\Category; 
use App\Tag; 
class AsideComposer{ 
    public function compose(View $view) 
    { 
    $categories=Category::all(); 
    $tags=Tag::all(); 
    $view->with('categories', $categories)->with('tags', $tags); 
    } 
} 

指数收益率Undefined variable:categories。任何想法?

+0

是你的AppServiceProvider中的'boot()'方法吗? – TheFallen

回答

1

然后在“供应商的阵列在你的配置/ app.php补充​​:

App\Providers\ComposerServiceProvider::class 
0

我有同样的问题。虽然返回变量视图,请尝试使用compact(),如:

$view->with(compact('variable1', 'variable2')); 

这种固定的错误我。