StudentController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 13
c 2
b 2
f 0
dl 0
loc 24
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A search() 0 21 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Gate;
8
9
class StudentController extends Controller
10
{
11
    /* Return search results for enrollment modal */
12
    public function search(Request $request)
13
    {
14
        // If the user is not allowed to perform this action
15
        if (Gate::forUser(backpack_user())->denies('enroll-students')) {
16
            abort(403);
17
        }
18
19
        $data = [];
20
21
        if ($request->has('q')) {
22
            $search = $request->q;
23
24
            $data = DB::table('students')
25
                    ->select('students.id', 'users.firstname', 'users.lastname')
26
                    ->join('users', 'students.id', '=', 'users.id')
27
                    ->where('users.firstname', 'LIKE', "%$search%")
28
                    ->orWhere('users.lastname', 'LIKE', "%$search%")
29
                    ->get();
30
        }
31
32
        return response()->json($data);
33
    }
34
}
35