lundi 28 juin 2021

is there a better way to write this if statement in laravel

good day i am new in laravel and i am still learning as i go.

my question is this code works but i feel like it unnecessary and can be written much better please can someone assist would a switch case be better or is there a better way to write this and please explain to me thank you.

debtorscontroller.php

    $debtors_created_at = $debtors->created_at;
    $now = Carbon::now();
    $created_at_in_days = $debtors_created_at->diffInDays($now);


     if ($created_at_in_days >= 0 and $created_at_in_days <= 10){

         Debtor::whereId($debtorid)->update(['collection_level' => 'Pre Collection']);
     }elseif ($created_at_in_days >= 11 and $created_at_in_days <= 30){

            Debtor::whereId($debtorid)->update(['collection_level' => 'Collection Level 1']);
        }
     elseif ($created_at_in_days >= 31 and $created_at_in_days <= 60){

         Debtor::whereId($debtorid)->update(['collection_level' => 'Collection Level 2']);
     }
     elseif ($created_at_in_days >= 61){

         Debtor::whereId($debtorid)->update(['collection_level' => 'Collection Level 3']);
     }

this just check wheter the debtor was created at and if it is 1-10 days old it changes

its level

days 1-10 is pre collection 11-30 is collection level 1 31-60 is collection level 2 61-90+ is collection level 3

this is a field in my debtors table collection_level

Aucun commentaire:

Enregistrer un commentaire