Composer View is not loading variable into view

亡梦爱人 提交于 2019-12-01 02:47:49

问题


I created 3 composer views previously and they all work properly, but then I created another one, which doesn't seem to work. I've been trying to get it to work, it doesn't seem to be something related to my code. I will drop a piece of it here, but I still don't think it's the code.

Provider EvenComposerProvider:

public function register(){
    $this->composeEven();
}
public function composeEven(){
    view()->composer('includes.aklinkosesi', 'App\Http\Composers\EvenComposer');
}

Composer EvenComposer:

class EvenComposer{
   public function compose(View $view){
      $view->with('evens', Even::orderBy('id','desc')->paginate(10));
   }
}

And than I included the provider inside app.php

App\Providers\EvenComposerProvider::class

When I try to loop through $evens with the foreach, it throws the error:

Undefined variable: evens

My rough guess will be that, Laravel does not compile app.php


回答1:


So here is the solutions which might work for people in same sitatuin. First try these commands:

composer update
php artisan config:clear
php artisan cache:clear 
composer dumpautoload
php artisan cache:clear

I tried a few of them than deleted bootstrap/cache/config file and it worked.




回答2:


You are doing it wrong, put this code into boot method inside of your provider and also, remove the code from register and remove your method composeEven:

View::composer(
   'layouts.aklinkosesi', 'App\Http\Composers\EvenComposer'
);


来源:https://stackoverflow.com/questions/48814573/composer-view-is-not-loading-variable-into-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!