phplaravelmodel-view-controllerproxmox

Error Message Illuminate\\Database\\Eloquent\\Collection::save does not exist. in Laravel


this is the code to save Proxmox API data to database. Detecting new record and updating a record automatically. the value of the $apiData, $requestData, $type , $mode, i aleardy check it all. so the problem is in the code below. and its always said "Illuminate\Database\Eloquent\Collection::save does not exist. in Laravel".

public function mapApiDataToModel($apiData, $requestData, $type , $mode)
{
     if ($mode == 'new'){
        $mappedData = new ProxmoxVps;
     }
     elseif ($mode == 'update'){
        $mappedData = ProxmoxVps::find($requestData['id_proxmox_vps']);
     }

    $mappedData->id_proxmox_node = $requestData['id_proxmox_node'];
    $mappedData->nama = $requestData['selected_node'];
    $mappedData->vm_id = $apiData['vmid'];
    $mappedData->type = $type;
    $mappedData->catatan = null;
    $mappedData->status = $apiData['status'];
    $mappedData->status_ews1 = $apiData['status_ews1'] ?? 'danger';
    $mappedData->status_ews2 = $apiData['status_ews2'] ?? 'normal';
    $mappedData->last_update = now();
    $mappedData->online = $apiData['online'] ?? 'online';
    $mappedData->cpu_usage_percentage = round($apiData['cpu'] * 100, 2);
    $mappedData->cpu_max = $apiData['cpus'];
    $mappedData->ram_usage_percentage = round(($apiData['mem'] / $apiData['maxmem']) * 100, 2);
    $mappedData->ram_max = $apiData['maxmem'] / (1024 * 1024 * 1024);
    $mappedData->hd_usage_percentage = round($apiData['disk'] / (1024 * 1024), 2);
    $mappedData->hd_max = round($apiData['maxdisk'] / (1024 * 1024), 2);
    $mappedData->hd_read_utilization = round($apiData['diskread'] / (1024 * 1024), 2);
    $mappedData->hd_write_utilization = round($apiData['diskwrite'] / (1024 * 1024), 2);
    $mappedData->net_in_utilization = round($apiData['netin'] / (1024 * 1024), 2);
    $mappedData->net_out_utilization = round($apiData['netout'] / (1024 * 1024), 2);

    if ($type === "lxc") {
        $mappedData->sched_checking_interval = $requestData[0]['listSelectedCT']['interval'] ?? 0;
    } else {
        $mappedData->sched_checking_interval = $requestData[0]['listSelectedVM']['interval'] ?? 0;
    }

    $mappedData->sched_ews_interval = $apiData['sched_ews_interval'] ?? 0;
    $mappedData->sched_is_processing = false;
    $mappedData->ews1_warning_threshold = $requestData['ews1_warning_threshold'];
    $mappedData->ews1_danger_threshold = $requestData['ews1_danger_threshold'];
    $mappedData->ews2_sampling_count = $requestData['ews2_sampling_count'];
    $mappedData->ews2_percentile_warning_threshold = $requestData['ews2_percentile_warning_threshold'];
    $mappedData->ews2_percentile_danger_threshold = $requestData['ews2_percentile_danger_threshold'] ?? 0;
    $mappedData->row_status = 1;
    $mappedData->deleted_by = null;
    $mappedData->deleted_at = null;
    $mappedData->updated_by = $apiData['updated_by'] ?? '';
    $mappedData->created_by = $apiData['created_by'] ?? '';

    DB::transaction(function () use ($mappedData) {
        $mappedData->save();
        Log::debug($mappedData);

    });

    return $mappedData;
} 

it's a litlle bit too complex for a newbie like me, i've tried so many ways to solve this, from changing command until the algorithm, but i got no change. please help me, i've try to solve this for a couple days😥😥


Solution

  • After all day I searched for an answer to this, it turns out the problem was in the value $requestData['id_proxmox_vps'] which was assigned to $mappedData = ProxmoxVps::find($requestData['id_proxmox_vps']); to get the ID for updating the record.

    This is the value:

    array:1 [ // app\Http\Controllers\Proxmox\ProxmoxNodeController.php:279
      0 => 135
    ]
    

    The variable must point directly to the value so that it can be read correctly

    $mappedData = ProxmoxVps::find($requestData['id_proxmox_vps'][0]);