Repository

Data warehouse (Repository) is a concrete implementation of the Dcat Admin interface for data add/drop operations. The intervention of Repository can make the construction of the page no longer care about the specific implementation of the data read and write functions. Developers through the implementation of the Repository interface can read and write operations on the data.

{tip} Of course, for your convenience the system also retains the ability to use Model directly, the underlying layer will automatically convert Model to a repository instance, after all, most of the time the direct use of Model can also meet our needs.

Components such as data table Grid, data form Form, data detail Show, Tree, etc. no longer depend directly on Model, but on a repository that provides simpler and clearer interfaces, the following are all the interfaces of Repository.

  1. <?php
  2. namespace Dcat\Admin\Contracts;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Show;
  6. use Illuminate\Support\Collection;
  7. interface Repository
  8. {
  9. /**
  10. * Get Primary Key Name.
  11. *
  12. * @return string
  13. */
  14. public function getKeyName();
  15. /**
  16. * Get creation time field
  17. *
  18. * @return string
  19. */
  20. public function getCreatedAtColumn();
  21. /**
  22. * Get update time field
  23. *
  24. * @return string
  25. */
  26. public function getUpdatedAtColumn();
  27. /**
  28. * Whether to use soft deletes
  29. *
  30. * @return bool
  31. */
  32. public function isSoftDeletes();
  33. /**
  34. * Get Grid Data
  35. *
  36. * @param Grid\Model $model
  37. *
  38. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Collection|array
  39. */
  40. public function get(Grid\Model $model);
  41. /**
  42. * Get Edit Page Data
  43. *
  44. * @param Form $form
  45. *
  46. * @return array|\Illuminate\Contracts\Support\Arrayable
  47. */
  48. public function edit(Form $form);
  49. /**
  50. * Get details page data
  51. *
  52. * @param Show $show
  53. *
  54. * @return array|\Illuminate\Contracts\Support\Arrayable
  55. */
  56. public function detail(Show $show);
  57. /**
  58. * New record
  59. *
  60. * @param Form $form
  61. *
  62. * @return mixed
  63. */
  64. public function store(Form $form);
  65. /**
  66. * Query the row data before updating
  67. *
  68. * @param Form $form
  69. *
  70. * @return array|\Illuminate\Contracts\Support\Arrayable
  71. */
  72. public function updating(Form $form);
  73. /**
  74. * Update data
  75. *
  76. * @param Form $form
  77. *
  78. * @return bool
  79. */
  80. public function update(Form $form);
  81. /**
  82. * Delete data
  83. *
  84. * @param Form $form
  85. * @param array $deletingData
  86. *
  87. * @return mixed
  88. */
  89. public function delete(Form $form, array $deletingData);
  90. /**
  91. * Row data before deletion
  92. *
  93. * @param Form $form
  94. *
  95. * @return array|\Illuminate\Contracts\Support\Arrayable
  96. */
  97. public function deleting(Form $form);
  98. }

If your data is multi-level structured, you will also need to implement the following interfaces

  1. <?php
  2. namespace Dcat\Admin\Contracts;
  3. interface TreeRepository
  4. {
  5. /**
  6. * Get the primary key field name.
  7. *
  8. * @return string
  9. */
  10. public function getPrimaryKeyColumn();
  11. /**
  12. * Get the parent ID field name.
  13. *
  14. * @return string
  15. */
  16. public function getParentColumn();
  17. /**
  18. * Get the TITLE field name
  19. *
  20. * @return string
  21. */
  22. public function getTitleColumn();
  23. /**
  24. * Get the sort field name
  25. *
  26. * @return string
  27. */
  28. public function getOrderColumn();
  29. /**
  30. * Saving hierarchical data sorting
  31. *
  32. * @param array $tree
  33. * @param int $parentId
  34. */
  35. public function saveOrder($tree = [], $parentId = 0);
  36. /**
  37. * Setting Data Query Callbacks
  38. *
  39. * @param \Closure|null $query
  40. *
  41. * @return $this
  42. */
  43. public function withQuery($queryCallback);
  44. /**
  45. * Get hierarchical data.
  46. *
  47. * @return array
  48. */
  49. public function toTree();
  50. }

Repository Interface

getKeyName

This interface requires the primary key field name of the returned data to return a value of type string.

  1. public function getKeyName()
  2. {
  3. return 'id';
  4. }

getCreatedAtColumn

This interface requires the created_at field name of the data to be returned, or an empty string or null value if there is no value.

  1. // Returns a null string or `null` value if there is no value
  2. public function getCreatedAtColumn()
  3. {
  4. return 'created_at';
  5. }

getUpdatedAtColumn

This interface requires the updated_at field name of the data to be returned, or an empty string or null value if there is no value.

  1. // Returns a null string or `null` value if there is no value
  2. public function getCreatedAtColumn()
  3. {
  4. return 'updated_at';
  5. }

isSoftDeletes

This interface requires the return of data to support soft delete or not, return a value of type bool.

  1. public function isSoftDeletes()
  2. {
  3. return true;
  4. }

get

This interface requires the return of data from the data table Grid for data table presentation and requires a value of type array, Illuminate\Support\Collection or LengthAwarePaginator.

pagination

Requires the return of a \Illuminate\Contracts\Pagination\LengthAwarePaginator type value when data is to be paginated.

  1. public function get(Grid\Model $model)
  2. {
  3. // Get the current number of pages
  4. $currentPage = $model->getCurrentPage();
  5. // Get the number of lines per page
  6. $perPage = $model->getPerPage();
  7. // Get filter parameters
  8. $city = $model->filter()->input(Grid\Filter\Scope::QUERY_NAME, 'Guangzhou');
  9. $start = ($currentPage - 1) * $perPage;
  10. $client = new \GuzzleHttp\Client();
  11. $response = $client->get("{$this->api}?{$this->apiKey}&city=$city&start=$start&count=$perPage");
  12. $data = json_decode((string)$response->getBody(), true);
  13. return $model->makePaginator(
  14. $data['total'] ?? 0, // Total number of incoming records
  15. $data['subjects'] ?? [] // Two-dimensional array of incoming data
  16. );
  17. }

no pagination

If paging is not required, simply return a value of type array or Illuminate\Support\Collection.

  1. public function get(Grid\Model $model)
  2. {
  3. return [
  4. ['id' => 1, 'name' => 'n1'],
  5. ['id' => 2, 'name' => 'n2']
  6. ];
  7. }

Note that grid requires paging to be disabled!

  1. $grid->disablePagination()

edit

This interface requires the return of data from a form edit page for displaying a data form edit page, and needs to return a value of type array.

  1. public function edit(Form $form): array
  2. {
  3. // Get data primary key values
  4. $id = $form->getKey();
  5. return ['id' => 1, 'name' => 'n1'];
  6. }

detail

This interface requires data from the Data Details page to be returned for displaying data details, and requires a value of type array to be returned.

  1. public function detail(Show $show): array
  2. {
  3. // Get data primary key values
  4. $id = $show->getId();
  5. return ['id' => 1, 'name' => 'n1'];
  6. }

store

This interface is used to add a new record and can return values of type int, string or bool.

  1. public function store(Form $form)
  2. {
  3. // Access to data to be added
  4. $attributes = $form->updates();
  5. // Implement your added logic.
  6. // Return the new record id or bool value.
  7. return 1;
  8. }

updating

This interface is used to query the raw record when the data form modifies the data, which needs to return a value of type array or Model.

{tip} This interface is only used for some special fields, such as image and file upload fields, so when an image or file is changed, the old file can be deleted based on the data retrieved from this interface. So if your form does not use such special fields, this interface can return an empty array.

  1. public function updating(Form $form)
  2. {
  3. // Get data primary key values
  4. $id = $form->getKey();
  5. return ['id' => 1, 'name' => 'n1'];
  6. }

update

This interface is used to modify records for data forms, and can return values of type int, string or bool.

  1. public function update(Form $form)
  2. {
  3. // Access to data to be edited
  4. $attributes = $form->updates();
  5. // Execute your editing logic
  6. // Return success
  7. return true;
  8. }

deleting

This interface is used to query the original record when deleting data, which requires the return of a two-dimensional array, or a Collection model.

  1. public function deleting(Form $form): array
  2. {
  3. // Multiple ids when batch deleting
  4. $id = explode(',', $form->getKey());
  5. // Implement your logic.
  6. // Note that a two-dimensional array needs to be returned here.
  7. return [
  8. ['id' => 1, 'name' => 'h1'],
  9. ];
  10. // You can also return a collection
  11. return Modell::find($id);
  12. }

destroy

The single/batch delete data method, success returns true, failure returns false.

  1. public function destroy(Form $form, array $deletingData)
  2. {
  3. // Multiple ids when batch deleting
  4. $id = explode(',', $form->getKey());
  5. // $deletingData is the data returned by the getDataWhenDeleting interface.
  6. // Implement your logic.
  7. return true;
  8. }

TreeRepository接口

getPrimaryKeyColumn

This interface is used to return the primary key field name of the data, which requires a value of type string to be returned.

  1. public function getPrimaryKeyColumn()
  2. {
  3. return $this->getKeyName();
  4. }

getParentColumn

This interface is used to return the parent ID field name of the data and requires a value of type string to be returned.

  1. public function getParentColumn()
  2. {
  3. return 'parent_id';
  4. }

getTitleColumn

This interface is used to return the data TITLE field name, which requires a value of type string to be returned.

  1. public function getTitleColumn()
  2. {
  3. return 'title';
  4. }

getOrderColumn

This interface is used to return the name of the data sorting field and requires a value of type string.

{tip} This field is not required, so if your data doesn’t support it or doesn’t need to be sorted, return an empty value!

  1. public function getOrderColumn()
  2. {
  3. return 'order';
  4. }

saveOrder

This interface is used to save the sorting of hierarchical data and receives two parameters

  • $tree array This field is a hierarchical array of fields
  • $parentId int This field is primarily used to pass parent IDs for recursion.

{tip} If your data does not support MySQL, you can refer to the Dcat\Admin\Traits\ModelTree::saveOrder method to do it yourself.

  1. $tree = [
  2. [
  3. 'id' => 1,
  4. 'title' => 'title',
  5. 'parent_id' => 0,
  6. 'children' => [
  7. [
  8. 'id' => 2,
  9. 'title' => 'child1',
  10. 'parent_id' => 1,
  11. ],
  12. [
  13. 'id' => 3,
  14. 'title' => 'child2',
  15. 'parent_id' => 1,
  16. ],
  17. ],
  18. ]
  19. ];
  20. // Save the sort, please implement your own internal logic.
  21. $repository->saveOrder($tree);

withQuery

This interface should be used in conjunction with the toTree interface to receive a parameter: a callback or parameter primarily used to set the data query operation.

  1. <?php
  2. use Dcat\Admin\Contracts\Repository;
  3. use Dcat\Admin\Contracts\TreeRepository;
  4. use Dcat\Admin\Support\Helper;
  5. class Category implements Repository, TreeRepository
  6. {
  7. protected $queryCallbacks = [];
  8. public function withQuery($queryCallback)
  9. {
  10. $this->queryCallbacks[] = $queryCallback;
  11. return $this;
  12. }
  13. public function toTree()
  14. {
  15. // The code shown here is just to illustrate what the withQuery method does.
  16. $client = ...;
  17. foreach ($this->queryCallbacks as $callback) {
  18. $callback($client);
  19. }
  20. return Helper::buildNestedArray($client->get());
  21. }
  22. }

toTree

This interface is mainly used for querying data and returning it at a hierarchical level, and needs to return array type values.

  1. public function toTree()
  2. {
  3. $client = ...;
  4. foreach ($this->queryCallbacks as $callback) {
  5. $callback($client);
  6. }
  7. return Helper::buildNestedArray($client->get());
  8. }

Models

Dcat Admin already has built-in support for Eloquent model, so if your data source supports Model, then you only need to inherit the Dcat\Admin\Repositories\EloquentRepository class to implement the CRUD operation on the data, such as.

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\EloquentRepository;
  4. use App\Models\Movie as MovieModel;
  5. class Movie extends EloquentRepository
  6. {
  7. // Just set your model class name here
  8. protected $eloquentClass = MovieModel::class;
  9. // With this method you can specify the field of the query, default "*"
  10. public function getGridColumns()
  11. {
  12. return [$this->getKeyName(), 'name', 'title', 'created_at'];
  13. }
  14. // With this method, you can specify the fields of the form page query, default "*"
  15. public function getFormColumns()
  16. {
  17. return [$this->getKeyName(), 'name', 'title', 'created_at'];
  18. }
  19. // With this method, you can specify the field for the data detail page query, which defaults to "*"
  20. public function getDetailColumns()
  21. {
  22. return ['*'];
  23. }
  24. }

QueryBuilder

If your data supports QueryBuilder query, but it is not convenient to build model classes (for example, you need to dynamically look up table data), you can inherit the Dcat\Admin\Repositories\QueryBuilderRepository class.

{tip} Note that QueryBuilderRepository does not support Model‘s associative model, soft delete, model tree and field sorting functions by default.

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\QueryBuilderRepository;
  4. class MyRepository extends QueryBuilderRepository
  5. {
  6. // Set the name of your primary key
  7. protected $keyName = 'id';
  8. // Set the Create Time field
  9. protected $createdAtColumn = 'created_at';
  10. // Set the update time field
  11. protected $updatedAtColumn = 'updated_at';
  12. // Return table name
  13. public function getTable()
  14. {
  15. return 'your_table_name';
  16. }
  17. // Return the name of your primary key.
  18. public function getKeyName()
  19. {
  20. return $this->keyName;
  21. }
  22. // With this method you can specify the field of the query, default "*"
  23. public function getGridColumns()
  24. {
  25. return [$this->getKeyName(), 'name', 'title', 'created_at'];
  26. }
  27. // With this method, you can specify the fields of the form page query, default "*"
  28. public function getFormColumns()
  29. {
  30. return [$this->getKeyName(), 'name', 'title', 'created_at'];
  31. }
  32. // With this method, you can specify the field for the data detail page query, which defaults to "*"
  33. public function getDetailColumns()
  34. {
  35. return ['*'];
  36. }
  37. }