Blame view

app/Http/Controllers/EducationController.php 2.17 KB
827280168   Сергей П   Добавлена страниц...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?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'));
      }
  }