Home > Mobile >  laravel im getting value in view table before 4day how to keep once get value show in table if date
laravel im getting value in view table before 4day how to keep once get value show in table if date

Time:04-11

Using this code I'm able to get data before 4days in view table, I want to display once I get data it present show any time in view table

 $dueDate = Carbon::now()->addDays(4);
 Payment::select( 'next_due_date')->whereDate('next_due_date', $dueDate)->get();

CodePudding user response:

If you want to save the state you have many options. Most convenient and fast ways are to save them in either cache or database itself.

Solution 1: saving in cache: (Receomented if you won't have too many rows in payments table)

$payments = Payment::select( 'next_due_date')
  ->where(function($q){
    // Get rows that are current next_due_date or is cached
    $q->whereDate('next_due_date', Carbon::now()->addDays(4))
      ->when(cache()->has('saved_payment_ids'), function(){
        $q->orWhereIn('id', cache()->get('saved_payment_ids') ?? []);
      });
  })
  ->get();

// Remember forever current rows in cache
cache()->forever('saved_payment_ids', $payments->pluck('id')->toArray());

Solution 2: saving in db (Receomented way)

/**
 * First you have to add boolean (tinyint) row in payments table
 * Lets call it: payment_due_date
 */
$payments = Payment::select( 'next_due_date')
  ->where(function($q){
    $q->whereDate('next_due_date', Carbon::now()->addDays(4))
      ->orWhere('payment_due_date', 1);
  })
  ->get();

// Update new rows to save for future fetching
Payment::query()
  ->where('payment_due_date', 0)
  ->whereDate('next_due_date', Carbon::now()->addDays(4))->update(['payment_due_date' => 1]);
  • Related