??????????????
<?php

namespace App\Http\Middleware;

use Closure;
use App\Artist;
use App\Picture;
use Auth;

class CookieMiddleware
{

    public function handle($request, Closure $next)
    {

        $type = (isset($_COOKIE['type'])) ? $_COOKIE['type'] : '';
        if (Auth::guest()) {
            $user_id = 0;
            $pic = Picture::where('type', $type)->orderBy('id', 'DESC')->first();
        } else {
            $user_id = Auth::user()->id;
            $pic_user = Picture::where('user_id', $user_id)->where('type', $type)->orderBy('id', 'DESC')->first();
            $pic_guest = Picture::where('type', $type)->orderBy('id', 'DESC')->first();
            if (isset($pic_user)) {
                $pic_id_user = $pic_user->id;
            } else {
                $pic_id_user = 0;
            }
            if (isset($pic_guest)) {
                $pic_id_guest = $pic_guest->id;
            } else {
                $pic_id_guest = 0;
            }
            if ($pic_id_user > $pic_id_guest) {
                $pic = $pic_user;
            } else {
                $pic = $pic_guest;
            }
        }


        if (isset($pic)) {
            $picture = $pic;
            $pipec = $pic->picture;
            $arr = @getimagesize($pic->picture);
        } else {
            $picture = new Picture;
            $pipec = $_SERVER['DOCUMENT_ROOT'] . '/public/uploader/1502341206-realism.png';
            $arr = @getimagesize($pipec);
        }
        $arr_w = isset($arr[0])?(int)$arr[0] / 38 : 1;
        $arr_h = isset($arr[1])?(int)$arr[1] / 38 : 1;
        if (isset($_GET['width'])) {
            $width = (int)$_GET['width'];
        }
        elseif(isset($_COOKIE['width_с'])){
            $width = (int)$_COOKIE['width_с'];
        } else {
            $width = (int)$arr_w;
        }
        if (isset($_GET['height'])) {
            $height = (int)$_GET['height'];
        }elseif(isset($_COOKIE['height_с'])){
            $height = (int)$_COOKIE['height_с'];
        } else {
            $height = (int)($width * $arr_h / $arr_w);
        }
        if($height == 0){
            $height = 1;
        }
        if (isset($_GET['artist_name'])) {
            $name = $_GET['artist_name'];
            $obj = Artist::where('name', $name)->first();
            $artist_id = $obj->id;
        } else {

            if (isset($_GET['artist_id'])) {
                $artist_id = (int)$_GET['artist_id'];
            } else {
                if (isset($_COOKIE['artist_id'])) {
                    $artist_id = (int)$_COOKIE['artist_id'];
                } else {
                    $artist_id = 0;
                }
            }
        }
        if (isset($_GET['saloon_id'])) {
            $saloon_id = (int)$_GET['saloon_id'];
        } else {
            if (isset($_COOKIE['saloon_id'])) {
                $saloon_id = (int)$_COOKIE['saloon_id'];
            } else {
                $saloon_id = 0;
            }
        }
        if (isset($_GET['catalogs_id'])) {
            $catalogs_id = (int)$_GET['catalogs_id'];
        } else {
            if (isset($_COOKIE['catalogs_id'])) {
                $catalogs_id = (int)$_COOKIE['catalogs_id'];
            } else {
                $catalogs_id = 0;
                $pic_obj = null;
            }
        }
        $request->merge(compact('picture', 'pipec', 'width', 'height', 'catalogs_id', 'artist_id', 'saloon_id'));
        setcookie("width", $width, time() + 3600, '/');
        setcookie("height", $height, time() + 3600, '/');
        setcookie("catalogs_id", $catalogs_id, time() + 3600, '/');
        setcookie("artist_id", $artist_id, time() + 3600, '/');
        setcookie("saloon_id", $saloon_id, time() + 3600, '/');
        setcookie("picture_id", $picture->id, time() + 3600, '/');
        setcookie("type", $type, time() + 3600, '/');
        return $next($reque