MainController.php 5.91 KB
<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use App\Models\Category;
use App\Models\Good;
use App\Models\Good_Look;
use App\Models\Images;
use App\Models\News;
use App\Models\Project;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Symfony\Component\HttpFoundation\Session\Storage;

class MainController extends Controller
{
    // Главная страница
    public function index(Request $request) {
        $banners = Banner::query()->orderBy('id')->get();
        $category = Category::query()->where('parent_id', '>', '0')->orderBy('id')->get();

        if (isset($request->filter)) {
            switch($request->filter) {
                case 'news':$goods = Good::query()->where('new', '=', '1')->orderBy('id')->paginate(8);
                    break;
                case 'stock':$goods = Good::query()->where('stock_count', '>', '0')->orderBy('id')->paginate(8);
                    break;
                case 'demo':$goods = Good::query()->where('demo', '=', '1')->orderBy('id')->paginate(8);
                    break;
                case 'way':$goods = Good::query()->where('way', '=', '1')->orderBy('id')->paginate(8);
                    break;
            }
        } else {
            $goods = Good::query()->where('new', '=', '1')->orderBy('id')->paginate(8);
        }

        if ($request->ajax()) {
            return view('index_catalog', compact('goods'));
        }

        $news = News::query()->where('status', '=', 'новость')->orderByDesc('id')->paginate(3);
        return view('index',
            compact('banners', 'category', 'goods', 'news'));
    }

    public function about_company() {
        return view('company');
    }

    public function catalog(Request $request) {
        $items = Category::all();
        $goods = Good::query();
        $articles = News::query()->where('status', '=', 'статья')->paginate(10);
        $reviews = News::query()->where('status', '=', 'отзыв')->paginate(10);
        $lookin = Good_Look::query()->orderByDesc('id')->limit(4)->pluck('goods_id')
            ->toArray();
        $lookin_good = Good::query()->whereIn('id', $lookin)->get();

        if (!empty($request->filter)) {
            $filter = json_decode($request->filter);
            if (is_array($filter))
               if (count($filter) > 0)
                   $goods = $goods->whereIn('manufacturer', $filter);
        }

        if ($request->sort == 'count') {
            $goods = $goods->orderByDesc('stock_count')->orderByDesc('id');
        }

        if ($request->sort == 'price') {
            $goods = $goods->orderBy('price')->orderByDesc('id');
        }

        $goods = $goods->paginate(6);

        if ($request->ajax()) {
            return view('catalog_ajax', compact('goods'));
        }

        $mf = Good::select('manufacturer')->distinct()->pluck('manufacturer');
        return view('catalog', compact('items', 'goods',
            'mf', 'articles', 'reviews', 'lookin_good'));
    }

    public function catalog_detail(Category $category, Request $request) {
        $items = Category::all();
        $title = $category->name;
        $goods = Good::query()->where('category_id', '=', $category->id);
        $articles = News::query()->where('status', '=', 'статья')->paginate(10);
        $reviews = News::query()->where('status', '=', 'отзыв')->paginate(10);
        $lookin = Good_Look::query()->orderByDesc('id')->limit(4)->pluck('goods_id')
            ->toArray();
        $lookin_good = Good::query()->whereIn('id', $lookin)->get();

        if (!empty($request->filter)) {
            $filter = json_decode($request->filter);
            if (is_array($filter))
                if (count($filter) > 0)
                    $goods = $goods->whereIn('manufacturer', $filter);
        }

        if ($request->sort == 'count') {
            $goods = $goods->orderByDesc('stock_count')->orderByDesc('id');
        }

        if ($request->sort == 'price') {
            $goods = $goods->orderBy('price')->orderByDesc('id');
        }

        $goods = $goods->paginate(6);

        if ($request->ajax()) {
            return view('catalog_ajax', compact('goods'));
        }

        $mf = Good::select('manufacturer')->distinct()->where('category_id', '=', $category->id)->pluck('manufacturer');
        return view('catalog_detail', compact('items', 'category', 'goods',
            'title', 'mf', 'articles', 'reviews', 'lookin_good'));
    }

    public function good(Good $good) {
        $looking = new Good_Look();
        $looking->goods_id = $good->id;
        $looking->save();

        $lookin = Good_Look::query()->orderByDesc('id')->limit(4)->pluck('goods_id')
            ->toArray();
        $lookin_good = Good::query()->whereIn('id', $lookin)->get();

        $projects = Project::query()->orderByDesc('id')->paginate(10);

        $sample = Images::query()->where('sample', '=', '1')->where('good_id', '=', $good->id)->get();
        $images = Images::query()->where('sample', '=', '0')->where('good_id', '=', $good->id)->get();

        $accessory = Good::query()->where('accessory_id', '=', $good->id)->limit(8)->get();
        $tooling = Good::query()->where('tooling_id', '=', $good->id)->limit(8)->get();
        $also_good = Good::query()->where('category_id', '=', $good->category_id)->limit(4)->get();

        return view('good', compact('good', 'accessory', 'tooling', 'also_good',
            'sample', 'images', 'lookin_good', 'projects'));
    }

    public function simple_good(Good $good) {
        $looking = new Good_Look();
        $looking->goods_id = $good->id;
        $looking->save();

        $lookin = Good_Look::query()->orderByDesc('id')->limit(4)->pluck('goods_id')
            ->toArray();
        $lookin_good = Good::query()->whereIn('id', $lookin)->get();

        return view('simple_good', compact('good', 'lookin_good'));
    }

    public function online() {
        return redirect()->back();
    }
}