This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

stalinmanuXL
stalinmanuXL

Hi. In my table I have JSON fields. When I have created my YAML file it displays the data as follows. DATOS is a JSON

  • ID
  • NAME
  • DATOS[NAME]
  • DATOS[DESCRIPCION]

I have created a simple export to a CSV file. JSON fields are seen based on the YAML file.

But when doing a more custom export the JSON fields are no longer displayed.

¿What can I do to display the JSON fields and the fields within the JSON.?

mjauvin
mjauvin

what do you mean by "more custom export"? show some code.

stalinmanuXL
stalinmanuXL

Sorry for not being able to answer earlier.

CODE YAML

  • ID
  • NAME
  • DATOS.NAME
  • DATOS.RESUME
  • DATOS.DESCRIPCION
  • DATOS.DATE

DATOS: is a JSON

BASIC RESULT: ID,NAME,{"NAME":"VALUE","RESUME","VALUE","DESCRIPCION":"VALUE","DATE":"VALUE"}

CODE CUSTOM EXPORT

public function exportData($columns, $sessionKey = null)
{

    $models = MODEL::all();

    $models->each(function($model) use ($columns) {

        foreach ($columns as $column) {

            $pos = strpos($column,'.');
            // Format Column: array.value

            if($pos == true){

                $strA = mb_substr($column,0,$pos);
                $strC = substr($column, $pos+1);

                if (is_array($model->$strA)) {

                    $datos    = '';
                    $datosArray = $model->$strA;
                    if(isset($datosArray[$strC])) {
                        $datos = strip_tags($datosArray[$strC]); 
                    }else{
                        $datos = " "; 
                    }
                    $model->$column = $datos;
                    $model->addVisible($column);
                }else{
                    $model->addVisible($column);
                }
            } else {
                $model->$column = strip_tags($model->$column);
                $model->addVisible($column);
            }
        }
    });

    return $models->toArray();
}

RESULT:

FILE CSV:

ID,NAME,NAME,RESUME,DESCRIPCION, DATE

CONVERT JSON TO CSV.

I THINK.

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.