MyServiceProvider.php 3.76 KB
<?php

namespace App\Providers;

use App\Models\Company;
use App\Models\ContentRoles;
use App\Models\Employer;
use App\Models\Job_title;
use App\Models\Worker;
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;
               $is_manager = Auth::user()->is_manager;
               $admin = Auth::user()->admin;

               $contents = ContentRoles::query()->get();

               $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,
                            'is_manager' => $is_manager,
                            'admin' => $admin,
                            'contents' => $contents]);
           }
        );

        $views3 = ['layout.frontend', 'index'];

        View::composer($views3,
            function($view){
                $id = Auth::user();
                $companies = Company::query()->limit(1)->get();
                $jobs = Job_title::query()->where('is_remove', '=', '0')->get();
                $view->with(['UserId' => $id, 'companies' => $companies, 'jobs' => $jobs]);
            }
        );

        $view1 = ['employers.menu', 'employers.emblema', 'employers.send_all'];
        View::composer($view1,
            function($view){
                $user = Auth::user();
                $id = $user->id;
                $employer = Employer::query()->where('user_id', $id)->get();
                $id_employer = $employer[0]->id;
                $view->with(['id_employer' => $id_employer, 'Employer' => $employer]);
            }
        );

        $view_worker = ['workers.emblema'];
        View::composer($view_worker,
                     function($view_worker) {
                         $user = Auth::user();
                         $id = $user->id;
                         $Worker = Worker::query()->where('user_id', $id)->get();
                         $id_worker = $Worker[0]->id;
                         $view_worker->with(['id_worker' => $id_worker, 'Worker' => $Worker, 'User' => $user]);
                     }
        );

        $view_job_title = ['modals.register'];
        View::composer($view_job_title,
            function($view_job_title) {
                $Job_titles = Job_title::query()->where('is_remove', '=', '0')->get();
                $view_job_title->with(['Job_titles' => $Job_titles]);
            }
        );

    }
}