我正在构建一个数据表(yajra)来从spatie/laravel-activitylog
数据库表中提取数据。到目前为止一切正常,但properties
数据库中的列是 json。好吧,如果我将 json 直接绘制到数据表中,我将得到一个 [object Object]。有什么方法可以在特定列中美化 json 吗?
数据表列代码:
columns: [
{ data: 'causer_type' },
{ data: 'causer_id' },
{ data: 'subject_type' },
{ data: 'subject_id' },
{ data: 'description' },
{ data: 'properties' },
{ data: 'created_at' },
{ data: 'updated_at' },
],
控制器功能:
// Get data
$dump = Activity::all();
// Return datatable
return DataTables::of($dump)->make(true);
properties
转储示例:
"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"}}
我试图对列进行原始编辑,但由于某种原因总是返回 null ......
任何帮助将不胜感激!谢谢。
编辑#1我想出打印数据,但作为字符串。真的不知道可数据列是否可以存储/显示 json 数据...
// 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);