EducationController.php 2.17 KB
<?php

namespace App\Http\Controllers;

use App\Models\Education;
use App\Models\ProgramEducation;
use Illuminate\Http\Request;

class EducationController extends Controller
{
    // Образование
    public function index(Request $request) {
        $educations = Education::query();
        if (($request->has('search')) && (!empty($request->get('search')))) {
            $search = trim($request->get('search'));
            $educations = $educations->where('name', 'LIKE', "%$search%");
        }

        if ($request->get('sort')) {
            $sort = $request->get('sort');
            switch ($sort) {
                case 'name_up': $educations = $educations->orderBy('name')->orderBy('id'); break;
                case 'name_down': $educations = $educations->orderByDesc('name')->orderby('id'); break;
                case 'created_at_up': $educations = $educations->OrderBy('created_at')->orderBy('id'); break;
                case 'created_at_down': $educations = $educations->orderByDesc('created_at')->orderBy('id'); break;
                case 'default': $educations = $educations->orderBy('id')->orderby('updated_at'); break;
                default: $educations = $educations->orderBy('id')->orderby('updated_at'); break;
            }
        }

        $count_edu = $educations->count();
        $educations = $educations->paginate(6);
        if ($request->ajax()) {
            return view('ajax.education', compact('educations'));
        } else {
            return view('education.index', compact('educations', 'count_edu'));
        }
    }

    public function show(Education $education){
        $education_programs = ProgramEducation::query()
            ->where('education_id', '=', $education->id)
            ->get()
        ;
        // Группировка коллекции по полю 'level'
        $levels_education_programs = $education_programs->groupBy('level')->map(function ($items, $key) {
            return $items->map(function ($item) {
                return $item->toArray();
            })->toArray();
        })->toArray();
//dd($levels_education_programs);
        return view('education.show', compact('education', 'levels_education_programs'));
    }
}