So I made a website for an employment agency but the search function for the vacancies doesn’t work properly. When I try to search nothing to test how many vacancies are available when searching there are only 24 vacancies and it needs to be around 380. I’m using Laravel 11. Can someone help me with this?
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHttp;
use IlluminateSupportFacadesCache;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesDB;
use OscabreraQueryFiltersUtilitiesQueryFilters;
class VacatureController extends Controller
{
private function fetchAndCacheVacancies()
{
$cacheKey = 'all_vacancies';
$cacheDuration = 30 * 60;
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$jobs = [];
$branches = [];
$regions = [];
$educationLevels = [];
$headers = [
'Accept' => 'application/json',
'authorization' => 'my api key'
];
try {
$responseLookups = Http::withHeaders($headers)->get('https://api.forceflow.nl/connect/lookups', [
'language' => 'nl',
]);
if ($responseLookups->successful()) {
$lookups = $responseLookups->json();
$branches = $lookups['industry'] ?? [];
$regions = $lookups['region'] ?? [];
$educationLevels = $lookups['educationLevel'] ?? [];
Cache::put('lookups', $lookups, 24 * 60 * 60); // 24 hours
}
} catch (Exception $e) {
Log::error('Error fetching lookups: ' . $e->getMessage());
}
$offset = 0;
while (true) {
DB::reconnect();
try {
$responseJobs = Http::withHeaders($headers)->get('https://api.forceflow.nl/connect/jobs', [
'offset' => $offset
]);
if ($responseJobs->successful()) {
$fetchedJobs = $responseJobs->json();
if (empty($fetchedJobs)) {
break;
}
$jobs = array_merge($jobs, $fetchedJobs);
$offset += 200;
} else {
break;
}
} catch (Exception $e) {
Log::error('Error fetching jobs: ' . $e->getMessage());
break;
}
}
$totalVacancies = count($jobs);
$cachedData = [
'totalVacancies' => $totalVacancies,
'branches' => $branches,
'regions' => $regions,
'educationLevels' => $educationLevels,
'jobs' => $jobs,
];
Cache::put($cacheKey, $cachedData, $cacheDuration);
return $cachedData;
}
public function search(Request $request)
{
$keyword = $request->input('keyword');
$location = $request->input('location');
$distance = $request->input('distance');
$page = $request->input('page', 1);
$perPage = 20;
$cacheKey = 'search_' . md5(serialize([$keyword, $location, $distance, $page]));
$cacheDuration = 30 * 60;
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$headers = [
'Accept' => 'application/json',
'authorization' => 'my api key'
];
$offset = 0;
$jobs = [];
$branches = [];
$regions = [];
$educationLevels = [];
try {
$responseLookups = Http::withHeaders($headers)->get('https://api.forceflow.nl/connect/lookups', [
'language' => 'nl',
]);
if ($responseLookups->successful()) {
$lookups = $responseLookups->json();
$branches = $lookups['industry'] ?? [];
$regions = $lookups['region'] ?? [];
$educationLevels = $lookups['educationLevel'] ?? [];
Cache::put('lookups', $lookups, 24 * 60 * 60); // 24 hours
}
} catch (Exception $e) {
Log::error('Error fetching lookups: ' . $e->getMessage());
}
while (true) {
try {
$responseJobs = Http::withHeaders($headers)->get('https://api.forceflow.nl/connect/jobs', [
'offset' => $offset,
]);
if ($responseJobs->successful()) {
$fetchedJobs = $responseJobs->json();
if (empty($fetchedJobs)) {
break; // No more jobs available
}
$jobs = array_merge($jobs, $fetchedJobs);
$offset += 200; // Increment offset for next page
} else {
break; // Error fetching jobs
}
} catch (Exception $e) {
Log::error('Error fetching jobs: ' . $e->getMessage());
break; // Exception occurred, stop fetching
}
}
$filteredJobs = collect($jobs)->filter(function ($job) use ($keyword, $location, $distance) {
if ($keyword && !empty($keyword)) {
if (stripos($job['title'], $keyword) === false) {
return false;
}
}
if ($location && !empty($location)) {
if (!isset($job['location']) || stripos($job['location'], $location) === false) {
return false;
}
}
if (!empty($distance) && isset($job['distance'])) {
if ($job['distance'] > $distance) {
return false;
}
}
return true;
})->toArray();
// Pagination
$totalVacancies = count($filteredJobs);
$totalPages = ceil($totalVacancies / $perPage);
$currentPageJobs = array_slice($filteredJobs, ($page - 1) * $perPage, $perPage);
$view = view('vacatures', [
'jobs' => $currentPageJobs,
'branches' => $branches,
'regions' => $regions,
'educationLevels' => $educationLevels,
'currentPage' => $page,
'totalPages' => $totalPages,
'totalVacancies' => $totalVacancies,
])->render();
// Cache the rendered view
Cache::put($cacheKey, $view, $cacheDuration);
return $view;
}
public function getTotalVacatures(Request $request)
{
$cachedData = $this->fetchAndCacheVacancies();
$perPage = 20;
$page = $request->query('page', 1);
$start = ($page - 1) * $perPage;
$currentPageJobs = array_slice($cachedData['jobs'], $start, $perPage);
$totalPages = ceil($cachedData['totalVacancies'] / $perPage);
return view('vacatures', [
'totalVacancies' => $cachedData['totalVacancies'],
'branches' => $cachedData['branches'],
'regions' => $cachedData['regions'],
'educationLevels' => $cachedData['educationLevels'],
'jobs' => $currentPageJobs,
'currentPage' => $page,
'totalPages' => $totalPages,
]);
}
}```
I have tried using packages for filtering vacancies and I have tried to use the Laravel filter function.
New contributor
yeloquin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.