ShowCreator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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', true) : $timestamps;
  15. $rows = [];
  16. if ($primaryKey) {
  17. $rows[] = " \$show->{$primaryKey};";
  18. }
  19. foreach ($fields as $k => $field) {
  20. if (empty($field['name'])) continue;
  21. $rows[] = " \$show->{$field['name']};";
  22. if ($k === 1 && (count($fields) > 2 || $timestamps)) {
  23. $rows[] = " \$show->divider();";
  24. }
  25. }
  26. if ($timestamps) {
  27. $rows[] = " \$show->created_at;";
  28. $rows[] = " \$show->updated_at;";
  29. }
  30. return trim(implode("\n", $rows));
  31. }
  32. }