【问题标题】:Laravel 5: View Composer and Service Provider not workingLaravel 5:视图作曲家和服务提供者不工作
【发布时间】:2017-12-26 07:06:40
【问题描述】:

我想在多个视图中显示数据库中的一些数据。我为此目的使用 View Composer 和服务提供程序,但它不起作用。

这是我的 app/Http/ViewComposers/CategorycountComposer.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);
    }
}

这是我的 app/Providers/CategorycountServiceProvider.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()
    {
        //
    }
}

这是 m config/app.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,

    ],

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

<ul class="list-group" style="border: none">
<li class="list-group-item" style="background-color:#f2f2f2"><strong>Search by Category</strong></li>
@foreach($categoryCount as $category) 
<li class="list-group-item">{{$category->category_title}} 
<span class="badge badge-pill badge-primary">{{$category->totall</span> 
</li> 
@endforeach
</ul>

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

未定义变量:categoryCount(查看: C:\AppServ\www\getajob\project\resources\views\layouts\categ‌​ory_panel.blade.php) (看法: C:\AppServ\www\getajob\project\resources\views\layouts\categ‌​ory_panel.blade.php)

【问题讨论】:

  • 你正在覆盖你的 categoryCount
  • 我没有得到你。我如何覆盖 foreach??
  • 这样使用@foreach($categoryCount as $category) &lt;li class="list-group-item"&gt;{{$category-&gt;category_title}} &lt;span class="badge badge-pill badge-primary"&gt;{{$category-&gt;totall&lt;/span&gt; &lt;/li&gt; @endforeach
  • 它仍然给我同样的错误:未定义的变量:categoryCount(查看:C:\AppServ\www\getajob\project\resources\views\layouts\category_panel.blade.php)(查看:C :\AppServ\www\getajob\project\resources\views\layouts\category_panel.blade.php)

标签: php laravel laravel-5 service-provider


【解决方案1】:

你应该使用composer dump-autoload

【讨论】:

  • 您应该提供解释。为什么使用这条线?为什么 OP 解决方案不起作用?
猜你喜欢
  • 2015-06-14
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 2018-08-06
  • 2014-08-02
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多