Laravel 5:View Composer和服务提供商无法正常工作

时间:2017-07-20 14:09:22

标签: php laravel laravel-5 service-provider

我想在多个视图中显示数据库中的一些数据。我正在使用View Composer和服务提供商来实现此目的,但它无法正常工作。

这是我的app / Http / ViewComposers / CategorycountComposer.php文件

users_db

这是我的app / Providers / CategorycountServiceProvider.php文件

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;

use DB;

use App\General_model;

class CategorycountComposer
{
    public $categoriescount = [];

    public function __construct()
    {
        $query = "SELECT ct.`category_title`,ct.`category_id`,(SELECT COUNT(`job_id`) FROM `job_details` WHERE `category_id` = ct.`category_id`) AS totall FROM `job_category` AS ct ORDER BY ct.`category_title` ASC";
        $this->categoriescount = General_model::rawQuery($query);
    }


    public function compose(View $view)
    {
        $view->with('categoryCount',$this->categoriescount);
    }
}

这是m config / app.php文件

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CategorycountServiceProvider extends ServiceProvider
{

    public function boot()
    {
         view()->composer(
            ['layouts.category_panel','jobs.jobdetails'],
            'App\Http\ViewComposers\CategorycountComposer'
        );
    }


    public function register()
    {
        //
    }
}

这是我的layouts.category_panel.blade.php文件

  'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
        App\Providers\CategorycountServiceProvider::class,

        /*
         * Package Service Providers...
         */
        Laravel\Tinker\TinkerServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

现在,每次我尝试打开视图时,都会出现以下错误

  

未定义的变量:categoryCount(查看:   C:\的appserv \ WWW \ getajob \项目\资源\意见\布局\ CATEG ory_panel.blade.php)   (视图:   C:\的appserv \ WWW \ getajob \项目\资源\视图\布局\ CATEG ory_panel.blade.php)

1 个答案:

答案 0 :(得分:-1)

您应该使用composer dump-autoload