PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Laravel PHP: Get values out of Recursion Function

 February 16, 2022     eloquent, laravel, laravel-5, php     No comments   

Issue

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?


Solution

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);  
}


Answered By - nice_dev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing