Changeset 7e9dadd for app/Http/Controllers/PeopleController.php
- Timestamp:
- 02/12/24 14:17:23 (11 months ago)
- Branches:
- main
- Children:
- 2bd3041
- Parents:
- 92df8cd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/PeopleController.php
r92df8cd r7e9dadd 23 23 return view('login'); 24 24 } 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 25 32 $credentials = request()->validate([ 26 'embg' => 'required' 33 'embg' => 'nullable', // Assuming embg is not always required 34 'gender' => 'nullable', 35 'age' => 'nullable', 27 36 ]); 28 $ embg = $credentials['embg'];37 $query = 'SELECT * FROM people WHERE true'; 29 38 30 $peoples = DB::select('SELECT * FROM people WHERE embg ~ :embg', ['embg' => '^' . $embg]); 39 $embg = '^' . $credentials['embg']; 40 if ($credentials['embg']) { 41 $query .= " AND embg LIKE '{$credentials['embg']}%'"; 42 } 31 43 32 return view('filter', [ 33 'peoples' => $peoples 34 ]); 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]); 35 73 } 36 74 public function getPerson(Request $request)
Note:
See TracChangeset
for help on using the changeset viewer.