PlatformContentController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Controllers\V2;
  3. use App\Http\Controllers\BaseController;
  4. use App\Repositories\PlatformContentRepository;
  5. use App\Transformers\PlatformContentTransformer;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Validation\Rule;
  9. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  10. use League\Fractal\Resource\Collection;
  11. use League\Fractal\Manager;
  12. /**
  13. * Created by PhpStorm.
  14. * User: durong
  15. * Date: 2019/6/17
  16. * Time: 下午2:04
  17. */
  18. class PlatformContentController extends BaseController
  19. {
  20. public function __construct(PlatformContentRepository $platformContentRepository)
  21. {
  22. $this->platformContentRepository = $platformContentRepository;
  23. }
  24. //平台内容列表
  25. public function index(Request $request)
  26. {
  27. $platformContent = $this->platformContentRepository->index($request->all());
  28. $fractal = new Manager();
  29. $resource = new Collection($platformContent, new PlatformContentTransformer());
  30. $resource->setPaginator(new IlluminatePaginatorAdapter($platformContent));
  31. $data = $fractal->createData($resource)->toArray();
  32. $data['extra'] = [
  33. 'filters' => [
  34. 'id',
  35. ],
  36. 'columns' => [
  37. 'id',
  38. 'title',
  39. 'content',
  40. 'updated_at'
  41. ]
  42. ];
  43. return $data;
  44. }
  45. //新增平台内容
  46. public function create(Request $request)
  47. {
  48. $validator = Validator::make($request->all(), [
  49. 'title' => 'required|string',
  50. 'content' => 'required|string',
  51. ]);
  52. if ($validator->fails()) {
  53. return $this->response->error($validator->errors()->first(), 500);
  54. }
  55. return $this->platformContentRepository->create($request->all());
  56. }
  57. //修改平台内容
  58. public function edit(Request $request)
  59. {
  60. $validator = Validator::make($request->all(), [
  61. 'id' => 'required|exists:platform_content',
  62. 'content' => 'required',
  63. 'title' => 'string',
  64. ]);
  65. if ($validator->fails()) {
  66. return $this->response->error($validator->errors()->first(), 500);
  67. }
  68. return $this->platformContentRepository->edit($request->all());
  69. }
  70. }