0

I have a invoice_schedule_table which is structured like:
id, previous_invoice_schedule_id, due_date
7559, , 01-06-2021
7772, 7559, 15-06-2021
7773, 7772, 20-06-2021

I am trying to create a looping/recursive function which will ultimately give me back the last due_date (20-06-2021) if I am opening up any invoice schedule(ID: 7559, 7772) from the UI.

function latestRescheduledInvoiceSchedule($invoiceScheduleId) {

    $rescheduledPaymentSchedule = InvoiceSchedule::where('previous_invoice_schedule_id', $invoiceScheduleId)->first();

    if(isset($rescheduledPaymentSchedule)) {
        latestRescheduledInvoiceSchedule($rescheduledPaymentSchedule->id);
    }
}

But I am unable to figure out how to get the value once it reaches the last record.

Any help regarding this?

1
  • Can't you just break once you reach a record where previous_invoice_schedule_id is null? Commented May 14, 2021 at 15:05

2 Answers 2

2

You should retrieve the due_date value when the $rescheduledPaymentSchedule is not set, in this case it will be with an id of 7773:

function latestRescheduledInvoiceSchedule($invoiceScheduleId) {
    $rescheduledPaymentSchedule = InvoiceSchedule::where('previous_invoice_schedule_id', $invoiceScheduleId)->first();

    if($rescheduledPaymentSchedule) {
        latestRescheduledInvoiceSchedule($rescheduledPaymentSchedule->id);
    } else {
        // There is no further values, retrieve the `due_date` of the current invoiceScheduleId
        $last_record = InvoiceSchedule::find($invoiceScheduleId);
        if ($last_record) {
            $last_due_date = $last_record->due_date;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

No need of recursive function. You can just use a loop of your choice to get the latest row like below:

<?php


function latestRescheduledInvoiceSchedule($invoiceScheduleId) {
    $payment_schedule_row = '';
    $rescheduledPaymentSchedule = '';
    do{
        $invoiceScheduleId = $rescheduledPaymentSchedule->id ?? $invoiceScheduleId;
        $payment_schedule_row = $rescheduledPaymentSchedule ?? '';
        $rescheduledPaymentSchedule = InvoiceSchedule::where('previous_invoice_schedule_id', $invoiceScheduleId)->first();
    }while(!empty($rescheduledPaymentSchedule));

    
    if(empty($payment_schedule_row)){
        $payment_schedule_row = InvoiceSchedule::find($invoiceScheduleId);
    }
    // rest of the code goes here
    dd($payment_schedule_row);  
}

2 Comments

Thanks. Worked like a charm. Infact, did not get my head around that this can be achieved through a while loop.
@DebarunPal Great. I would still suggest you to look into dev.mysql.com/doc/refman/8.0/en/with.html to reduce roundtrips to DB server with with recursive cte approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.