I want to show some data from database in multiple views. I am using View Composer and Service provider for this purposes but it is not working.
This is my app/Http/ViewComposers/CategorycountComposer.php file
<?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);
}
}
This is my app/Providers/CategorycountServiceProvider.php file
<?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()
{
//
}
}
This is m config/app.php file
'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,
],
This is my layouts.category_panel.blade.php file
<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>
Now every time I try to open the view, it gives me the following error
Undefined variable: categoryCount (View: C:\AppServ\www\getajob\project\resources\views\layouts\category_panel.blade.php) (View: C:\AppServ\www\getajob\project\resources\views\layouts\category_panel.blade.php)