add mutators to jsonb columns

pull/26/head
Ondřej Hruška 6 years ago
parent 7746d85e5f
commit df0e06045f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 6
      app/Http/Controllers/TableController.php
  2. 4
      app/Models/Revision.php
  3. 4
      app/Models/Row.php
  4. 2
      app/Tables/BaseExporter.php
  5. 3
      resources/views/table/_rows.blade.php

@ -44,7 +44,7 @@ class TableController extends Controller
return view('table.view', [ return view('table.view', [
'table' => $tableModel, 'table' => $tableModel,
'revision' => $revision, 'revision' => $revision,
'columns' => Column::columnsFromJson(json_decode($revision->columns)), 'columns' => Column::columnsFromJson($revision->columns),
'rows' => $revision->rows()->paginate(25), 'rows' => $revision->rows()->paginate(25),
]); ]);
} }
@ -204,7 +204,7 @@ class TableController extends Controller
$parsed[$key] = $columns[$i]->cast($val); $parsed[$key] = $columns[$i]->cast($val);
} }
return [ return [
'data' => json_encode($parsed), 'data' => $parsed,
]; ];
}, $rowTable); }, $rowTable);
@ -215,7 +215,7 @@ class TableController extends Controller
$revisionFields = [ $revisionFields = [
'note' => "Initial revision of table $u->name/$input->name", 'note' => "Initial revision of table $u->name/$input->name",
'columns' => json_encode($columns), 'columns' => $columns,
'row_count' => count($rowsData), 'row_count' => count($rowsData),
]; ];

@ -25,6 +25,10 @@ class Revision extends BaseModel
use HasRelaquentRelationships; use HasRelaquentRelationships;
protected $guarded = []; protected $guarded = [];
protected $casts = [
'columns' => 'object',
];
/** Included rows */ /** Included rows */
public function rows() public function rows()
{ {

@ -10,6 +10,10 @@ namespace App\Models;
*/ */
class Row extends BaseModel class Row extends BaseModel
{ {
protected $casts = [
'data' => 'object',
];
protected $guarded = []; protected $guarded = [];
public $timestamps = false; public $timestamps = false;
} }

@ -131,7 +131,7 @@ abstract class BaseExporter
$rows = $revision->rows()->offset($start)->limit($chunkSize)->get(); $rows = $revision->rows()->offset($start)->limit($chunkSize)->get();
foreach ($rows as $row) { foreach ($rows as $row) {
$data = json_decode($row->data); $data = $row->data;
// column renaming, value formatting... // column renaming, value formatting...

@ -18,9 +18,8 @@
<tbody> <tbody>
@foreach($rows as $row) @foreach($rows as $row)
<tr> <tr>
@php($rdata = json_decode($row['data'], true))
@foreach($columns as $col) @foreach($columns as $col)
<td data-id="{{ $row->id }}">{{ $rdata[$col->name] }}</td> <td data-id="{{ $row->id }}">{{ $row['data']->{$col->name} }}</td>
@endforeach @endforeach
</tr> </tr>
@endforeach @endforeach

Loading…
Cancel
Save