6.7.3. A Product Controller

Implementation of the product controller is similar to what we did for the customer controller:

  1. <?php
  2. /*
  3. * Product Controller
  4. */
  5. namespace App\Http\Controllers;
  6. use App\Http\Controllers\Controller;
  7. use App\Product;
  8. class ProductController extends Controller {
  9. /**
  10. * Displays a list of products
  11. *
  12. * @return Response
  13. */
  14. public function showProducts() {
  15. // Connect widget for search
  16. $filter = \DataFilter::source(new Product);
  17. // The search will be by product name
  18. $filter->add('NAME', 'Name', 'text');
  19. $filter->submit('Search');
  20. $filter->reset('Reset');
  21. // Create a grid to display the filtered data
  22. $grid = \DataGrid::source($filter);
  23. // output grid columns
  24. // Field, label, sorting
  25. $grid->add('NAME', 'Name', true);
  26. // Set the format with 2 decimal places
  27. $grid->add('PRICE|number_format[2,., ]', 'Price');
  28. $grid->row(function($row) {
  29. // Press the money values to the right
  30. $row->cell('PRICE')->style("text-align: right");
  31. });
  32. // Add buttons to view, edit and delete records
  33. $grid->edit('/product/edit', 'Edit', 'show|modify|delete');
  34. // Add the Add product button
  35. $grid->link('/product/edit', "?????????? ??????", "TR");
  36. // set sorting
  37. $grid->orderBy('NAME', 'asc');
  38. // set the number of records per page
  39. $grid->paginate(10);
  40. // display the customer template and pass the filter and grid to it
  41. return view('product', compact('filter', 'grid'));
  42. }
  43. /**
  44. * Add, edit and delete products
  45. *
  46. * @return Response
  47. */
  48. public function editProduct() {
  49. if (\Input::get('do_delete') == 1)
  50. return "not the first";
  51. // create editor
  52. $edit = \DataEdit::source(new Product());
  53. // Set the title of the dialog, depending on the type of operation
  54. switch ($edit->status) {
  55. case 'create':
  56. $edit->label('Add product');
  57. break;
  58. case 'modify':
  59. $edit->label('Edit product');
  60. break;
  61. case 'do_delete':
  62. $edit->label('Delete product');
  63. break;
  64. case 'show':
  65. $edit->label("Product's card");
  66. $edit->link('products', 'Back', 'TR');
  67. break;
  68. }
  69. // set that after the operations of adding, editing and deleting,
  70. // you need to return to the list of products
  71. $edit->back('insert|update|do_delete', 'products');
  72. // We add editors of a certain type, assign them a label and
  73. // associate them with the attributes of the model
  74. $edit->add('NAME', 'Name', 'text')->rule('required|max:100');
  75. $edit->add('PRICE', 'Price', 'text')->rule('max:19');
  76. $edit->add('DESCRIPTION', 'Description', 'textarea')
  77. ->attributes(['rows' => 8])
  78. ->rule('max:8192');
  79. // display the template product_edit and pass it to the editor
  80. return $edit->view('product_edit', compact('edit'));
  81. }
  82. }