Transform record before import to database

I wish to store a list of CSV columns as a JSON object in a single column of database. What is the best way to do it?

I want to store the following 3 columns of CSV: price, quantity, and brokerage as a JSON object in the data column of the record. I've tried setting the value in beforeFill as below, but it doesn't work.

    protected function beforeFill()
    {
        $this->data['data'] = [
            'price' => $this->data['price'],
            'brokerage' => $this->data['brokerage'],
            'quantity' => $this->data['quantity'],
        ];
        unset(
            $this->data['price'],
            $this->data['brokerage'],
            $this->data['quantity'],
        );
    }
Solution
Managed to solve it by appending the following code at the end of beforeFill:
        if ($this->record) {
            $this->record->data = $this->data['data'];
        }
Was this page helpful?