gpt4 book ai didi

php - 拉维尔 5 : View Composer and Service Provider not working

转载 作者:行者123 更新时间:2023-12-04 22:08:26 24 4
gpt4 key购买 nike

我想在多个 View 中显示数据库中的一些数据。为此,我正在使用 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>

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

Undefined variable: categoryCount (View: C:\AppServ\www\getajob\project\resources\views\layouts\categ‌​ory_panel.blade.php) (View: C:\AppServ\www\getajob\project\resources\views\layouts\categ‌​ory_panel.blade.php)

最佳答案

你应该使用 composer dump-autoload

关于php - 拉维尔 5 : View Composer and Service Provider not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45217175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com