简体   繁体   中英

How to get data from previous record in laravel

I have a table called items . Now, I'm getting all the records of the current week (means record each day). I've already done that.

But the problem is, if there is no record on let's say Wednesday, the data of Wednesday should still be the data of Tuesday Or the date before the Wednesday. Please see my code below.

public function arrayAssigner($theArray, $theArrayData, $field)
{
    $format_to_day = function($date){
        return date_format(new \DateTime($date), 'D');
    };

    foreach($theArray as $key => $value){
        if(isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Mon'){
            $theArray[0] = $theArrayData[$key][$field];
        } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Tue'){
            $theArray[1] = $theArrayData[$key][$field];
        } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Wed'){
            $theArray[2] = $theArrayData[$key][$field];
        } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Thu'){
            $theArray[3] = $theArrayData[$key][$field];
        } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Fri'){
            $theArray[4] = $theArrayData[$key][$field];
        }
    }

    return $theArray;
}

I used it like this:

$theArray = [0, 0, 0, 0, 0];
$this->arrayAssigner($theArray, $dataOfTheWeek, 'quantity');

I also have a constructor to set the weekStart at MONDAY .

Example :

Let's say I have a record from Nov 12 - 16 . Now, the current week is Nov 26-30 , but in the database I don't have a record on Nov 26 , it means, the record or the quantity of Nov 26 is the record from Nov 16 .

Output (as array) :
Nov 12 - 16:
[7, 5, 6, 8, 12]

Nov 26 - 30:
[12, 8, 9, 10, 13]

The first item in the array of Nov 26 - 30 should be 12, since it is the previous data of Nov 12 - 16 .

Support Image:

在此处输入图片说明

The result must be dynamic; Once there is a day that has no record, it will filled with the last record.

public function arrayAssigner($theArray, $theArrayData, $field) {

    $format_to_day = function($date){
        return date_format(new \DateTime($date), 'D');
    };

    foreach($theArray as $key => $value) {
        if(isset($theArrayData[$key])) {
            $day = $format_to_day($theArrayData[$key]['created_at']);

            switch ($day) {
                case 'Mon':
                    $theArray[0] = $theArrayData[$key][$field];
                break;
                case 'Tue':
                    // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                    $theArray[1] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[0];
                break;
                case 'Wed':
                    // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                    $theArray[2] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[1];
                break;
                case 'Thu':
                    // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                    $theArray[3] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[2];
                break;
                case 'Fri':
                    // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                    $theArray[4] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[3];
                break;
                default:
                break;
            }
        }
    }

    return $theArray;
}

Is this what you want???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM