[75151c6] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers;
|
---|
| 4 |
|
---|
| 5 | use Illuminate\Http\Request;
|
---|
[cf84baa] | 6 | use Illuminate\Support\Facades\DB;
|
---|
[d9c4096] | 7 | use Illuminate\Support\Facades\Session;
|
---|
[75151c6] | 8 |
|
---|
| 9 | class PeopleController extends Controller
|
---|
| 10 | {
|
---|
| 11 | function filter(){
|
---|
[d9c4096] | 12 | if(Session::get('pe_id') == null) {
|
---|
| 13 | return view('login');
|
---|
| 14 | }
|
---|
[cf84baa] | 15 | $peoples = DB::select('select * from people;');
|
---|
| 16 |
|
---|
| 17 | return view('filter', [
|
---|
| 18 | 'peoples' => $peoples
|
---|
| 19 | ]);
|
---|
| 20 | }
|
---|
| 21 | function filter_post(){
|
---|
[d9c4096] | 22 | if(Session::get('pe_id') == null) {
|
---|
| 23 | return view('login');
|
---|
| 24 | }
|
---|
[7e9dadd] | 25 | // $credentials = request()->validate([
|
---|
| 26 | // 'embg' => 'required'
|
---|
| 27 | // ]);
|
---|
| 28 | // $embg = $credentials['embg'];
|
---|
| 29 | //
|
---|
| 30 | // $peoples = DB::select('SELECT * FROM people WHERE embg ~ :embg', ['embg' => '^' . $embg]);
|
---|
| 31 |
|
---|
[cf84baa] | 32 | $credentials = request()->validate([
|
---|
[7e9dadd] | 33 | 'embg' => 'nullable', // Assuming embg is not always required
|
---|
| 34 | 'gender' => 'nullable',
|
---|
| 35 | 'age' => 'nullable',
|
---|
[cf84baa] | 36 | ]);
|
---|
[7e9dadd] | 37 | $query = 'SELECT * FROM people WHERE true';
|
---|
[cf84baa] | 38 |
|
---|
[7e9dadd] | 39 | $embg = '^' . $credentials['embg'];
|
---|
| 40 | if ($credentials['embg']) {
|
---|
| 41 | $query .= " AND embg LIKE '{$credentials['embg']}%'";
|
---|
| 42 | }
|
---|
[cf84baa] | 43 |
|
---|
[7e9dadd] | 44 | // Check if $credentials['gender'] is an array and handle accordingly
|
---|
| 45 | if (isset($credentials['gender']) && (is_array($credentials['gender']) && count($credentials['gender']) > 0)) {
|
---|
| 46 | $genderConditions = implode(" OR ", array_map(function ($gender) {
|
---|
| 47 | return "gender = '{$gender}'";
|
---|
| 48 | }, $credentials['gender']));
|
---|
| 49 |
|
---|
| 50 | $query .= " AND ({$genderConditions})";
|
---|
| 51 | } elseif (isset($credentials['gender']) && !is_array($credentials['gender'])) {
|
---|
| 52 | $query .= " AND gender = '{$credentials['gender']}'";
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | // Check if $credentials['age'] is an array and handle accordingly
|
---|
| 56 | if (isset($credentials['age']) && is_array($credentials['age']) && count($credentials['age']) > 0) {
|
---|
| 57 | $ageConditions = [];
|
---|
| 58 |
|
---|
| 59 | foreach ($credentials['age'] as $ageRange) {
|
---|
| 60 | // Extract minimum and maximum ages from the range
|
---|
| 61 | list($minAge, $maxAge) = explode('-', $ageRange);
|
---|
| 62 |
|
---|
| 63 | // Add condition for the age range
|
---|
| 64 | $ageConditions[] = "EXTRACT(YEAR FROM AGE(current_date, date_of_birth)) BETWEEN {$minAge} AND {$maxAge}";
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | $query .= " AND (" . implode(" OR ", $ageConditions) . ")";
|
---|
| 68 | }
|
---|
| 69 | // Use a raw SQL query with the built conditions
|
---|
| 70 |
|
---|
| 71 | $peoples = DB::select($query);
|
---|
| 72 | return view('filter', ['peoples' => $peoples]);
|
---|
[75151c6] | 73 | }
|
---|
[92df8cd] | 74 | public function getPerson(Request $request)
|
---|
| 75 | {
|
---|
| 76 | $embg = $request->input('embg');
|
---|
| 77 | $person = DB::select('SELECT * FROM people WHERE embg = :embg', ['embg' => $embg]);
|
---|
| 78 |
|
---|
| 79 | return response()->json($person[0] ?? null);
|
---|
[d9c4096] | 80 | }
|
---|
[75151c6] | 81 | }
|
---|