【发布时间】: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\category_panel.blade.php) (看法: C:\AppServ\www\getajob\project\resources\views\layouts\category_panel.blade.php)
【问题讨论】:
-
你正在覆盖你的 categoryCount
-
我没有得到你。我如何覆盖 foreach??
-
这样使用
@foreach($categoryCount as $category) <li class="list-group-item">{{$category->category_title}} <span class="badge badge-pill badge-primary">{{$category->totall</span> </li> @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