家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductController.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Product;
  5. class ProductController extends Controller
  6. {
  7. /**
  8. * 创建活动
  9. * @param Request $request
  10. * @return \Illuminate\Http\JsonResponse
  11. */
  12. public function store(Request $request)
  13. {
  14. $id = $request->input('id');
  15. $name = $request->input('name');
  16. $info = $request->input('info');
  17. $classify = $request->input('classify');
  18. $color = $request->input('color');
  19. $price = $request->input('price');
  20. $status = $request->input('status');
  21. if ($id) {
  22. $product = Product::find($id);
  23. $product->status = $status;
  24. } else {
  25. $product = new Product;
  26. $product->status = 0;
  27. $product->collect_num = 0;
  28. $product->sell_num = 0;
  29. }
  30. if ($name && $info && $classify && $color) {
  31. $product->name = $name;
  32. $product->info = $info;
  33. $product->color = $color;
  34. $product->price = $price;
  35. $product->classify = $classify;
  36. $product->save();
  37. return Response()->json([
  38. "status" => 0,
  39. "message" => "保存成功!"
  40. ]);
  41. } else {
  42. return Response()->json([
  43. "status" => -1,
  44. "message" => "信息不能为空!"
  45. ]);
  46. }
  47. }
  48. /**
  49. * 每页20进行分页
  50. * @param Request $request
  51. * @return mixed
  52. */
  53. public function list(Request $request)
  54. {
  55. $products = Product::all()->toArray();
  56. $datas = array_map(function ($product) {
  57. return [
  58. "id" => $product["id"],
  59. "classify" => $product["classify"],
  60. "name" => $product["name"],
  61. "price" => $product["price"]
  62. ];
  63. }, $products);
  64. return $datas;
  65. }
  66. }