Form data source

Models and data warehousing

Repository (Repository) is a class that can provide a specific interface to read and write operations on the data, through the introduction of repositories, you can make the construction of the page no longer care about the specific implementation of data read and write functions, developers only need to implement a specific interface to easily switch data sources. For detailed usage of the data warehouse, please refer to the document Repository.

Data from the model

{tip} If your data comes from Model, then you can also use the Model instance directly, and the underlying layer will automatically convert Model into a repository instance.

When the data source supports the model, it is sufficient to create a very simple `Repository’ class.

  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. // Define your model class name here
  8. protected $eloquentClass = MovieModel::class;
  9. // With this method, you can specify the fields of the form page query, default "*"
  10. public function getFormColumns()
  11. {
  12. return [$this->getKeyName(), 'name', 'title', 'created_at'];
  13. }
  14. }

usage:

  1. use App\Admin\Repositories\Movie;
  2. $form = new Form(new Movie);
  3. ...

Data from external API

The following is an example of the Douban movie API to demonstrate the usage of the Repository interface related to read and write operations on form data.

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use Dcat\Admin\Repositories\Repository;
  4. use Dcat\Admin\Form;
  5. class ComingSoon extends Repository
  6. {
  7. protected $api = 'https://api.douban.com/v2/movie/coming_soon';
  8. // Returns the name of your id field, default "id"
  9. protected $keyName = '_id';
  10. // Query edit page data
  11. // This method needs to return an array.
  12. public function edit(Form $form)
  13. {
  14. // Get id
  15. $id = $form->builder()->getResourceId();
  16. $data = file_get_contents("http://api.douban.com/v2/movie/subject/$id");
  17. return json_decode($data, true);
  18. }
  19. // This method is used to query the original record before modifying the data.
  20. // If a file upload form is used, the old file will be automatically deleted based on this original record when the file is changed.
  21. // If this data is not needed, just return an empty array.
  22. public function getDataWhenUpdating(Form $form)
  23. {
  24. // Get id
  25. $id = $form->builder()->getResourceId();
  26. return [];
  27. }
  28. // Modify operation
  29. // Returns a bool type data.
  30. public function update(Form $form)
  31. {
  32. // Get id
  33. $id = $form->builder()->getResourceId();
  34. // Get the data to be modified
  35. $attributes = $form->updates();
  36. // TODO
  37. // Write your modification logic here.
  38. return true;
  39. }
  40. // This method is used to query the original data before modifying it.
  41. // If a file upload form is used, files are automatically deleted based on this data.
  42. // If this data is not needed, just return an empty array.
  43. public function getDataWhenDeleting(Form $form)
  44. {
  45. $id = $form->builder()->getResourceId();
  46. $id = explode(',', $id);
  47. // $data = file_get_contents("http://api.douban.com/v2/movie/subject/$id");
  48. //
  49. // return json_decode($data, true);
  50. return [];
  51. }
  52. // Delete data
  53. // $deletingData is the data returned by the getDataWhenDeleting method.
  54. public function destroy(Form $form, $deletingData)
  55. {
  56. // Note that there may be multiple ids here.
  57. $id = $form->builder()->getResourceId();
  58. // When using the batch delete function, the id here is a string separated by ",".
  59. $id = explode(',', $id);
  60. // TODO
  61. // var_dump($id, $deletingData);
  62. return true;
  63. }
  64. }