2

I'm buiding a datatables (yajra) to draw data from spatie/laravel-activitylog database table. Everything works fine so far, but the properties column in the database is json. Well, if I draw json directly into datatable I will get an [object Object]. Is there any way to prettify json in the specific column?

Datatable columns code:

columns: [
    { data: 'causer_type' },
    { data: 'causer_id' },
    { data: 'subject_type' },
    { data: 'subject_id' },
    { data: 'description' },
    { data: 'properties' },
    { data: 'created_at' },
    { data: 'updated_at' },
],

The controller function:

// Get data
$dump = Activity::all();
// Return datatable
return DataTables::of($dump)->make(true);

An example of the properties dump:

"properties":{"old":{"email":"---","phone":"---","active":"1","country":"---","client_name":"---","date_of_birth":null,"delivery_town":"---","partner_number":null,"delivery_address":"---","delivery_zipcode":"---","facebook_profile":null,"partner_password":"---","personal_id_number":null,"accept_notifications":"1"},"attributes":{"email":"---","phone":"---","active":"1","country":"---","client_name":"---","date_of_birth":null,"delivery_town":"---","partner_number":null,"delivery_address":"---","delivery_zipcode":"---","facebook_profile":null,"partner_password":"---","personal_id_number":null,"accept_notifications":"1"}}

I have tried to raw edit the column but always returned null for some reason...

Any help will be appreciated! Thanks.

EDIT #1 I figure it out to print the data, but as string. Don't really know if a databable column can store/show json data...

// Get data
$dump = Activity::all();
// Prettify the properties column
foreach ($dump as $log)
{
    //dd(json_encode($log->properties, JSON_PRETTY_PRINT));
    $log->properties_pretty = json_encode($log->properties, JSON_PRETTY_PRINT);
}
// Return datatable
return DataTables::of($dump)->make(true);
apokryfos
  • 38,771
  • 9
  • 70
  • 114
hhelderneves
  • 925
  • 8
  • 24
  • Have you tried using [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) to convert the "properties" JSON into a string, before displaying it in your DataTables column? – andrewJames Apr 21 '20 at 23:53

0 Answers0