AttributeValueController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Controllers\Admin\Attributes;
  3. use App\Http\Controllers\Controller;
  4. use App\Shop\Attributes\Repositories\AttributeRepositoryInterface;
  5. use App\Shop\AttributeValues\AttributeValue;
  6. use App\Shop\AttributeValues\Repositories\AttributeValueRepository;
  7. use App\Shop\AttributeValues\Repositories\AttributeValueRepositoryInterface;
  8. use App\Shop\AttributeValues\Requests\CreateAttributeValueRequest;
  9. class AttributeValueController extends Controller
  10. {
  11. /**
  12. * @var AttributeRepositoryInterface
  13. */
  14. private $attributeRepo;
  15. /**
  16. * @var AttributeValueRepositoryInterface
  17. */
  18. private $attributeValueRepo;
  19. /**
  20. * AttributeValueController constructor.
  21. * @param AttributeRepositoryInterface $attributeRepository
  22. * @param AttributeValueRepositoryInterface $attributeValueRepository
  23. */
  24. public function __construct(
  25. AttributeRepositoryInterface $attributeRepository,
  26. AttributeValueRepositoryInterface $attributeValueRepository
  27. ) {
  28. $this->attributeRepo = $attributeRepository;
  29. $this->attributeValueRepo = $attributeValueRepository;
  30. }
  31. public function create($id)
  32. {
  33. return view('admin.attribute-values.create', [
  34. 'attribute' => $this->attributeRepo->findAttributeById($id)
  35. ]);
  36. }
  37. /**
  38. * @param CreateAttributeValueRequest $request
  39. * @param $id
  40. * @return \Illuminate\Http\RedirectResponse
  41. */
  42. public function store(CreateAttributeValueRequest $request, $id)
  43. {
  44. $attribute = $this->attributeRepo->findAttributeById($id);
  45. $attributeValue = new AttributeValue($request->except('_token'));
  46. $attributeValueRepo = new AttributeValueRepository($attributeValue);
  47. $attributeValueRepo->associateToAttribute($attribute);
  48. $request->session()->flash('message', 'Attribute value created');
  49. return redirect()->route('admin.attributes.show', $attribute->id);
  50. }
  51. /**
  52. * @param $attributeId
  53. * @param $attributeValueId
  54. * @return \Illuminate\Http\RedirectResponse
  55. */
  56. public function destroy($attributeId, $attributeValueId)
  57. {
  58. $attributeValue = $this->attributeValueRepo->findOneOrFail($attributeValueId);
  59. $attributeValueRepo = new AttributeValueRepository($attributeValue);
  60. $attributeValueRepo->dissociateFromAttribute();
  61. request()->session()->flash('message', 'Attribute value removed!');
  62. return redirect()->route('admin.attributes.show', $attributeId);
  63. }
  64. }