GridCreator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Dcat\Admin\Scaffold;
  3. trait GridCreator
  4. {
  5. /**
  6. * @param string $primaryKey
  7. * @param array $fields
  8. * @return string
  9. */
  10. protected function generateGrid(string $primaryKey = null, array $fields = [], $timestamps = null)
  11. {
  12. $primaryKey = $primaryKey ?: request('primary_key', 'id');
  13. $fields = $fields ?: request('fields', []);
  14. $timestamps = $timestamps === null ? request('timestamps', true) : $timestamps;
  15. $rows = [
  16. "\$grid->{$primaryKey}->bold()->sortable();"
  17. ];
  18. foreach ($fields as $field) {
  19. if (empty($field['name'])) continue;
  20. if ($field['name'] == $primaryKey) continue;
  21. $rows[] = " \$grid->{$field['name']};";
  22. }
  23. if ($timestamps) {
  24. $rows[] = " \$grid->created_at;";
  25. $rows[] = " \$grid->updated_at->sortable();";
  26. }
  27. $rows[] = <<<EOF
  28. \$grid->filter(function (Grid\Filter \$filter) {
  29. \$filter->equal('$primaryKey');
  30. });
  31. EOF;
  32. return implode("\n", $rows);
  33. }
  34. }