ShowCreator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Dcat\Admin\Scaffold;
  3. trait ShowCreator
  4. {
  5. /**
  6. * @param string $primaryKey
  7. * @param array $fields
  8. * @return string
  9. */
  10. protected function generateShow(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') : $timestamps;
  15. $rows = [];
  16. if ($primaryKey) {
  17. $rows[] = " \$show->field('{$primaryKey}');";
  18. }
  19. foreach ($fields as $k => $field) {
  20. if (empty($field['name'])) {
  21. continue;
  22. }
  23. $rows[] = " \$show->field('{$field['name']}');";
  24. // if ($k === 1 && (count($fields) > 2 || $timestamps)) {
  25. // $rows[] = ' $show->divider();';
  26. // }
  27. }
  28. if ($timestamps) {
  29. $rows[] = ' $show->field(\'created_at\');';
  30. $rows[] = ' $show->field(\'updated_at\');';
  31. }
  32. return trim(implode("\n", $rows));
  33. }
  34. }