I suppose the first question is what is the value of $this->request->get(‘patrol_type’)? There is a gap in your logic where there is a patrol_type but it is not one of the strings finals or distinguish.
Problems to get values from database
In my pistol-competition system we have something called patrols. There can be a number of shooters in each patrol. The patrol is build from a number of shooters. My problem is that when the competition is finished the results must be registered into the system for printouts. When I choose ”Results” no patrol shows up. Instead an error shows.
The system is upgraded from Laravel 5.3 to 7.29 and most of the functions is doing well but here it’s not working.
The error show this:
[2022-01-31 22:00:09] local.ERROR: Undefined variable: patrols {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined variable: patrols at /Users/ralph/laravelnew/webshooter_web_upgrade/app/Repositories/ResultsRepository.php:155)
[stacktrace]
#0 /Users/ralph/laravelnew/webshooter_web_upgrade/app/Repositories/ResultsRepository.php(155): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/Users/ralph/la...', 155, Array)
#1 /Users/ralph/laravelnew/webshooter_web_upgrade/app/Http/Controllers/Api/CompetitionsAdminResultsController.php(21): App\Repositories\ResultsRepository->getPatrols('2')
#2 /Users/ralph/laravelnew/webshooter_web_upgrade/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\Http\Controllers\Api\CompetitionsAdminResultsController->getResultsForRegistration(Object(Illuminate\Http\Request), '2')
#3 /Users/ralph/laravelnew/webshooter_web_upgrade/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('getResultsForRe...', Array)
#4 /Users/ralph/laravelnew/webshooter_web_upgrade/vendor/laravel/framework/src/Illuminate/Routing/Route.php(239): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Api\CompetitionsAdminResultsController), 'getResultsForRe...')
#5 /Users/ralph/laravelnew/webshooter_web_upgrade/vendor/laravel/framework/src/Illuminate/Routing/Route.php(196): Illuminate\Routing\Route->runController()
#6 /Users/ralph/laravelnew/webshooter_web_upgrade/vendor/laravel/framework/src/Illuminate/Routing/Router.php(685): Illuminate\Routing\Route->run()
The ResultsRepository.php (part):
return $signups;
}
public function getPatrols($competitionsId)
{
if(!$this->request->has('patrol_type')):
$patrols = Patrol::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
elseif($this->request->get('patrol_type') == 'finals'):
$patrols = PatrolFinals::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
elseif($this->request->get('patrol_type') == 'distinguish'):
$patrols = PatrolDistinguish::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
endif;
// 155:
return $patrols;
}
public function storeResults($competitionsId)
{
if($this->request->has('results')):
This is CompetitionsAdminResultsController.php:
<?php
namespace App\Http\Controllers\Api;
use App\Repositories\ResultsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class CompetitionsAdminResultsController extends Controller
{
public function __construct(ResultsRepository $results)
{
$this->middleware('checkUserCompetitionRole:results');
$this->results = $results;
}
public function getResultsForRegistration(Request $request, $competitionsId)
{
$signups = $this->results->getResultsForRegistration($competitionsId);
$stations = \App\Models\Station::where('competitions_id', $competitionsId)->whereBetween('sortorder', [$request->get('station_start'), $request->get('station_end')])->orderBy('sortorder')->get();
$patrols = $this->results->getPatrols($competitionsId);
return response()->json(['signups'=>$signups, 'stations'=>$stations, 'patrols'=>$patrols]);
}
public function storeResults(Request $request, $competitionsId)
{
$results = $this->results->storeResults($competitionsId);
return response()->json(_('Resultatet sparades'));
}
public function export(Request $request, $competitionsId)
{
if ($request->has('format') && $request->get('format') === 'xlsx') {
$filter = [
'std_medals' => $request->get('std_medals'),
'prices' => $request->get('prices'),
];
$this->results->generateXlsx($competitionsId, $filter);
} else {
$this->results->calculateResults($competitionsId);
$this->results->generatePdf($competitionsId);
}
}
public function exportTeamsResults(Request $request, $competitionsId)
{
$this->results->generateTeamsResultsPdf($competitionsId);
}
public function shootingcards(Request $request, $competitionsId)
{
$this->results->generatePdf($competitionsId);
}
}
It should read from the database where the patrols are stored. Wonder what could be wrong.
@raffelustig maybe the method should be like this:
public function getPatrols($competitionsId)
{
if ($this->request->get('patrol_type') == 'finals'):
return PatrolFinals::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
endif;
if ($this->request->get('patrol_type') == 'distinguish'):
return PatrolDistinguish::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
endif;
return Patrol::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
}
Now the method will always return something.
You could be more dynamic using match on PHP 8
public function getPatrols($competitionsId)
{
$patrol = match ($this->request->get('patrol_type')) {
'finals' => PatrolFinals::query(),
'distinguish' => PatrolDistinguish::query()
default => Patrol::query(),
};
return $patrol->where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
}
Please or to participate in this conversation.