Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

insight's avatar

Doubt in Query logic in Laravel 10

Dear Friends, I have a form structure as shown below

alt text

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

0 likes
6 replies
Snapey's avatar

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

insight's avatar

Dr @Snapey , Yes , dates are in proper format . Start date and end date is checked in validartion itself .

My requirement will share with some examples In my code short code is a determining factor some cases in adding data

  1. if I add a short code for eg : "FD" and it's start date is 01/01/2024 and end date is 04/04/2024 case 1 : if I try to add another short code "FD" in date between 01/01/2024 and end date is 04/04/2024 (both date inclusive) it WILL NOT ALLOW case 2: If try to add short code "FD" start date is 05/04/2024 (or end date can any higher value or not) it WILL ALLOW case 3: If I have a short code "FD" and it's end date is NULL, then WILL NOT ALLOW to add new entry with "FD" short code

Please advise

Thanks

Anes P A

insight's avatar
insight
OP
Best Answer
Level 2

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

1 like
Snapey's avatar

your code as it stands does not allow for empty end date.

you also no longer need 'subquery'

Usually when checking for an overlap you need to check for dates spanning the start and dates spanning the end, dates spanning both start and end, dates between start and end. You seem to test for only one condition. But as you are unable to describe the required business logic, this is not unsurprising.

Please or to participate in this conversation.