AddressController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Address;
  5. use App\Models\City;
  6. use App\Models\State;
  7. use Auth;
  8. class AddressController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. //
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. //
  27. }
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(Request $request)
  35. {
  36. $address = new Address;
  37. if($request->has('customer_id')){
  38. $address->user_id = $request->customer_id;
  39. }
  40. else{
  41. $address->user_id = Auth::user()->id;
  42. }
  43. $address->address = $request->address;
  44. $address->country_id = $request->country_id;
  45. $address->state_id = $request->state_id;
  46. $address->city_id = $request->city_id;
  47. $address->longitude = $request->longitude;
  48. $address->latitude = $request->latitude;
  49. $address->postal_code = $request->postal_code;
  50. $address->phone = $request->phone;
  51. $address->save();
  52. return back();
  53. }
  54. /**
  55. * Display the specified resource.
  56. *
  57. * @param int $id
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function show($id)
  61. {
  62. //
  63. }
  64. /**
  65. * Show the form for editing the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function edit($id)
  71. {
  72. $data['address_data'] = Address::findOrFail($id);
  73. $data['states'] = State::where('status', 1)->where('country_id', $data['address_data']->country_id)->get();
  74. $data['cities'] = City::where('status', 1)->where('state_id', $data['address_data']->state_id)->get();
  75. $returnHTML = view('frontend.partials.address_edit_modal', $data)->render();
  76. return response()->json(array('data' => $data, 'html'=>$returnHTML));
  77. // return ;
  78. }
  79. /**
  80. * Update the specified resource in storage.
  81. *
  82. * @param \Illuminate\Http\Request $request
  83. * @param int $id
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function update(Request $request, $id)
  87. {
  88. $address = Address::findOrFail($id);
  89. $address->address = $request->address;
  90. $address->country_id = $request->country_id;
  91. $address->state_id = $request->state_id;
  92. $address->city_id = $request->city_id;
  93. $address->longitude = $request->longitude;
  94. $address->latitude = $request->latitude;
  95. $address->postal_code = $request->postal_code;
  96. $address->phone = $request->phone;
  97. $address->save();
  98. flash(translate('Address info updated successfully'))->success();
  99. return back();
  100. }
  101. /**
  102. * Remove the specified resource from storage.
  103. *
  104. * @param int $id
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function destroy($id)
  108. {
  109. $address = Address::findOrFail($id);
  110. if(!$address->set_default){
  111. $address->delete();
  112. return back();
  113. }
  114. flash(translate('Default address can not be deleted'))->warning();
  115. return back();
  116. }
  117. public function getStates(Request $request) {
  118. $states = State::where('status', 1)->where('country_id', $request->country_id)->get();
  119. $html = '<option value="">'.translate("Select State").'</option>';
  120. foreach ($states as $state) {
  121. $html .= '<option value="' . $state->id . '">' . $state->name . '</option>';
  122. }
  123. echo json_encode($html);
  124. }
  125. public function getCities(Request $request) {
  126. $cities = City::where('status', 1)->where('state_id', $request->state_id)->get();
  127. $html = '<option value="">'.translate("Select City").'</option>';
  128. foreach ($cities as $row) {
  129. $html .= '<option value="' . $row->id . '">' . $row->getTranslation('name') . '</option>';
  130. }
  131. echo json_encode($html);
  132. }
  133. public function set_default($id){
  134. foreach (Auth::user()->addresses as $key => $address) {
  135. $address->set_default = 0;
  136. $address->save();
  137. }
  138. $address = Address::findOrFail($id);
  139. $address->set_default = 1;
  140. $address->save();
  141. return back();
  142. }
  143. }