where do you check the dates are in the required format?
where do you check that end date is after start date?
You will probably need to describe the business requirements if you want help rather than expecting people to reverse engineer your code
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Dear Friends, I have a form structure as shown below

I am trying to implement a logic while adding new entry with unique short_name as
public function GetPenaltyStatus(Request $request)
{
$start_date = $request->start_date;
$end_date = $request->end_date;
$short_name = $request->short_name;
$flag = 0; // if flag is 0 can add new entry if 1 did not allow
$startDate1 = $start_date;
$endDate1 = $start_date;
$startDate2 = $end_date;
$endDate2 = $end_date;
if ($end_date) {
$penalty = PenaltyMaster::where('short_name', $short_name)
->where(function ($query) use ($startDate1, $endDate1, $startDate2, $endDate2) {
$query->where(function ($subQuery) use ($startDate1, $endDate1) {
$subQuery->where('start_date', '<=', $startDate1)
->where('end_date', '>=', $endDate1);
})->orWhere(function ($subQuery) use ($startDate2, $endDate2) {
$subQuery->where('start_date', '<=', $startDate2)
->where('end_date', '>=', $endDate2);
});
})
->exists(); // one condition
if ($penalty) {
$flag = 1;
}
}
$penaltyRem = PenaltyMaster::where('short_name', $short_name)
->whereNull('end_date')
->exists();
if ($penaltyRem) {
$flag = 1;
}
return response()->json(['flag' => $flag]);
}
The above ajax logic checks if flag is 0 can add new entry if 1 did not allow.
But above query logic is not working . In my table has short_name as "D" has start_date 03/05/2024 and end_date 11/05/2024 but when I try to add a new short_name with name D with start_date 17-05-2024 and end_date as 19-05-2024 blocks and return 1... But in my case I need to add that entry to table .
My penalty_master table structure as
CREATE TABLE `penalty_master` (
`id` int(11) NOT NULL,
`penalty_name` varchar(255) NOT NULL,
`short_name` varchar(50) NOT NULL,
`status` varchar(3) NOT NULL DEFAULT '1',
`penalty_rate` float,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`updated_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `penalty_master`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `penalty_master`
--
ALTER TABLE `penalty_master`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
In this form , end date field is not MANDATORY. If for an entry that is blank , don't allow to entry that short code in table once more. In start_date , end_date fields entry is greater than current one can allow to save.
Any body please correct my code for a good logic
Thanks
Anes P A
Dear Friends ,
I got the solution . Thanks for restrospect my ideas .
my resultant controller function as
public function GetPenaltyStatus(Request $request)
{
$start_date = $request->start_date;
$end_date = $request->end_date;
$short_name = $request->short_name;
$flag = 0; // if flag is 0 can add new entry if 1 did not allow
// Check if there is any existing entry with the same short_name and overlapping date range
$penalty = PenaltyMaster::where('short_name', $short_name)
->where(function ($query) use ($start_date, $end_date) {
$query->where(function ($subQuery) use ($start_date, $end_date) {
$subQuery->where('start_date', '<=', $end_date)
->where('end_date', '>=', $start_date);
});
})
->exists();
// Check if there is any existing entry with the same short_name and NULL end_date
$penaltyRem = PenaltyMaster::where('short_name', $short_name)
->whereNull('end_date')
->exists();
if ($penalty || $penaltyRem) {
$flag = 1;
}
return response()->json(['flag' => $flag]);
}
Thanks
Anes P A
Please or to participate in this conversation.