MyServiceProvider.php 1.7 KB
<?php

namespace App\Providers;

use App\Models\Job_title;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class MyServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $views = ['admin.job_titles.parent_id'];
        View::composer($views,
            function($view)
            {
                static $items = null;

                if (is_null($items)) {
                    $items = Job_title::query()->
                                        orderByDesc('sort')->
                                        orderBy('name')->
                                        active()->
                                        get();
                    $parent = 0;
                    $view->with(['items' => $items, 'parent' => $parent]);
                } else {
                    $view->with(['items' => $items]);
                }

            }
        );

        $views2 = ['layout.admin', 'admin.index'];

        View::composer($views2,
           function($view){
               $id = Auth::user()->id;
               $query = DB::select(DB::raw('SELECT count(*) as MsgCount
                                    FROM messages m1
                                    Where ((m1.flag_new = 1) and (m1.to_user_id = :uid))
                                    '), ['uid' => $id]
               );

               $view->with(['MsgCount' => $query[0]->MsgCount, 'UserId' => $id]);
           }
        );
    }
}