PlanController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Plan\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\PlanResource;
  6. use App\Models\Asset;
  7. use App\Models\Plan;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. class PlanController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. */
  15. public function index(Request $request)
  16. {
  17. $plans = Plan::filter($request->all())->where("parent_id", 0)->with(['children'])->simplePaginate();
  18. return PlanResource::collection($plans);
  19. }
  20. /**
  21. * Store a newly created resource in storage.
  22. */
  23. public function store(CreateOrUpdateRequest $request)
  24. {
  25. $plan = new Plan();
  26. $plan->mergeFillable([
  27. 'company_id'
  28. ]);
  29. $plan->fill([
  30. ...$request->all(),
  31. 'company_id' => Auth::user()->company_id,
  32. ]);
  33. $plan->save();
  34. return $this->created();
  35. }
  36. /**
  37. * Display the specified resource.
  38. */
  39. public function show(string $id)
  40. {
  41. $plan = Plan::findOrFail($id);
  42. return new PlanResource($plan);
  43. }
  44. /**
  45. * Update the specified resource in storage.
  46. */
  47. public function update(CreateOrUpdateRequest $request, string $id)
  48. {
  49. $plan = Plan::findOrFail($id);
  50. $plan->fill($request->all());
  51. $plan->save();
  52. return $this->noContent();
  53. }
  54. /**
  55. * Remove the specified resource from storage.
  56. */
  57. public function destroy(string $id)
  58. {
  59. $plan = Plan::findOrFail($id);
  60. $plan->delete();
  61. return $this->noContent();
  62. }
  63. }