ShowCreator.php 1.1 KB

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