I have an if statement like this:
dump($teamIds);
if($teamIds){
$query = \App\Models\Team::with('Competition', 'Weapongroup');
$query->whereIn('teams.id', $teamIds);
$query->orderBy('competitions_id') -> orderBy('name');
$query->where(function($query) use ($club, $sender, $type){
$query->whereNull('invoices_id');
$query->where('clubs_id', $club->id);
$query->whereHas('Competition', function($query) use ($sender, $type){
$query->where('invoices_recipient_type', $type);
$query->where('invoices_recipient_id', $sender->id);
});
});
$teams = $query->get();
} else {
$teams = null;
}
Also $teams are not set to null because of that. $teamIds is an array and is set to "". The dump printout shows that:
------------ ----------------------------------------
date Sun, 08 May 2022 13:43:41 0200
controller "ClubInvoicesController"
source InvoiceRepository.php on line 191
file app/Repositories/InvoiceRepository.php
------------ ----------------------------------------
array:1 [
0 => ""
]
Despite this it enters the if procedure and creates an extra invoice for a team that is empty because there is no team present.. How to change the if($teamIds){ so it doesn't accept the ""? Or any other kind of solution. The Laravel system is upgraded from Laravel 5.3 where this code works and now to Laravel9 here.
CodePudding user response:
$teamIds is an array and is set to "".
This sentence makes no sense. "" is a string, not an array; it is a string containing no text, but it is still a string, just as 0 is still a number. [""]
is an array, but it is not empty - it contains a single element, which is that empty string. An empty array is one with no items in at all, []
.
Since you don't show where $teamIds is set, it's hard to be specific, but you probably want to write $teamIds = [];
somewhere.
If you can't change what the array looks like, you can of course look for that specific value, with if ( $teamIds === [''] )
but I'd stress that fixing it further up is probably better.
CodePudding user response:
Try empty function: if(!empty($teamIds)){