AddressController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Models\City;
  4. use App\Models\Country;
  5. use App\Http\Resources\V2\AddressCollection;
  6. use App\Models\Address;
  7. use App\Http\Resources\V2\CitiesCollection;
  8. use App\Http\Resources\V2\StatesCollection;
  9. use App\Http\Resources\V2\CountriesCollection;
  10. use Illuminate\Http\Request;
  11. use App\Models\Cart;
  12. use App\Models\State;
  13. class AddressController extends Controller
  14. {
  15. public function addresses()
  16. {
  17. return new AddressCollection(Address::where('user_id', auth()->user()->id)->get());
  18. }
  19. public function createShippingAddress(Request $request)
  20. {
  21. $address = new Address;
  22. $address->user_id = auth()->user()->id;
  23. $address->address = $request->address;
  24. $address->country_id = $request->country_id;
  25. $address->state_id = $request->state_id;
  26. $address->city_id = $request->city_id;
  27. $address->postal_code = $request->postal_code;
  28. $address->phone = $request->phone;
  29. $address->save();
  30. return response()->json([
  31. 'result' => true,
  32. 'message' => translate('Shipping information has been added successfully')
  33. ]);
  34. }
  35. public function updateShippingAddress(Request $request)
  36. {
  37. $address = Address::find($request->id);
  38. $address->address = $request->address;
  39. $address->country_id = $request->country_id;
  40. $address->state_id = $request->state_id;
  41. $address->city_id = $request->city_id;
  42. $address->postal_code = $request->postal_code;
  43. $address->phone = $request->phone;
  44. $address->save();
  45. return response()->json([
  46. 'result' => true,
  47. 'message' => translate('Shipping information has been updated successfully')
  48. ]);
  49. }
  50. public function updateShippingAddressLocation(Request $request)
  51. {
  52. $address = Address::find($request->id);
  53. $address->latitude = $request->latitude;
  54. $address->longitude = $request->longitude;
  55. $address->save();
  56. return response()->json([
  57. 'result' => true,
  58. 'message' => translate('Shipping location in map updated successfully')
  59. ]);
  60. }
  61. public function deleteShippingAddress($id)
  62. {
  63. $address = Address::where('id',$id)->where('user_id',auth()->user()->id)->first();
  64. if($address == null) {
  65. return response()->json([
  66. 'result' => false,
  67. 'message' => translate('Address not found')
  68. ]);
  69. }
  70. $address->delete();
  71. return response()->json([
  72. 'result' => true,
  73. 'message' => translate('Shipping information has been deleted')
  74. ]);
  75. }
  76. public function makeShippingAddressDefault(Request $request)
  77. {
  78. Address::where('user_id', auth()->user()->id)->update(['set_default' => 0]); //make all user addressed non default first
  79. $address = Address::find($request->id);
  80. $address->set_default = 1;
  81. $address->save();
  82. return response()->json([
  83. 'result' => true,
  84. 'message' => translate('Default shipping information has been updated')
  85. ]);
  86. }
  87. public function updateAddressInCart(Request $request)
  88. {
  89. try {
  90. Cart::where('user_id', auth()->user()->id)->update(['address_id' => $request->address_id]);
  91. } catch (\Exception $e) {
  92. return response()->json([
  93. 'result' => false,
  94. 'message' => translate('Could not save the address')
  95. ]);
  96. }
  97. return response()->json([
  98. 'result' => true,
  99. 'message' => translate('Address is saved')
  100. ]);
  101. }
  102. public function getCities()
  103. {
  104. return new CitiesCollection(City::where('status', 1)->get());
  105. }
  106. public function getStates()
  107. {
  108. return new StatesCollection(State::where('status', 1)->get());
  109. }
  110. public function getCountries(Request $request)
  111. {
  112. $country_query = Country::where('status', 1);
  113. if ($request->name != "" || $request->name != null) {
  114. $country_query->where('name', 'like', '%' . $request->name . '%');
  115. }
  116. $countries = $country_query->get();
  117. return new CountriesCollection($countries);
  118. }
  119. public function getCitiesByState($state_id,Request $request)
  120. {
  121. $city_query = City::where('status', 1)->where('state_id',$state_id);
  122. if ($request->name != "" || $request->name != null) {
  123. $city_query->where('name', 'like', '%' . $request->name . '%');
  124. }
  125. $cities = $city_query->get();
  126. return new CitiesCollection($cities);
  127. }
  128. public function getStatesByCountry($country_id,Request $request)
  129. {
  130. $state_query = State::where('status', 1)->where('country_id',$country_id);
  131. if ($request->name != "" || $request->name != null) {
  132. $state_query->where('name', 'like', '%' . $request->name . '%');
  133. }
  134. $states = $state_query->get();
  135. return new StatesCollection($states);
  136. }
  137. }