Skip to content

How to import a json column #3816

Discussion options

You must be logged in to vote

Found the solution, basically, the following things need to be considered:

  • The field containing JSON data needs to be decoded
  • The model should cast these fields as an array

Laravel models convert data to arrays to insertion, but if the fields in DB are JSON fields the model will automatically serialize the values

Reference: https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting

Example of my code:

/**
* @throws \JsonException
*/
public function model(array $row)
{
$this->affectedRows++;
$parameters = [];
$i = 0;
foreach ($this->columns as $column => $value) {
if ($value) {
$parameters[$column] = $value;
} elseif ($this->isJson($row[$i])) {
$parameters[$column] = json_deco…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by lucasaguiar86
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant