123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace App\Http\Controllers\Api\V2;
- use App\Models\City;
- use App\Models\Country;
- use App\Http\Resources\V2\AddressCollection;
- use App\Models\Address;
- use App\Http\Resources\V2\CitiesCollection;
- use App\Http\Resources\V2\StatesCollection;
- use App\Http\Resources\V2\CountriesCollection;
- use Illuminate\Http\Request;
- use App\Models\Cart;
- use App\Models\State;
- class AddressController extends Controller
- {
- public function addresses()
- {
- return new AddressCollection(Address::where('user_id', auth()->user()->id)->get());
- }
- public function createShippingAddress(Request $request)
- {
- $address = new Address;
- $address->user_id = auth()->user()->id;
- $address->address = $request->address;
- $address->country_id = $request->country_id;
- $address->state_id = $request->state_id;
- $address->city_id = $request->city_id;
- $address->postal_code = $request->postal_code;
- $address->phone = $request->phone;
- $address->save();
- return response()->json([
- 'result' => true,
- 'message' => translate('Shipping information has been added successfully')
- ]);
- }
- public function updateShippingAddress(Request $request)
- {
- $address = Address::find($request->id);
- $address->address = $request->address;
- $address->country_id = $request->country_id;
- $address->state_id = $request->state_id;
- $address->city_id = $request->city_id;
- $address->postal_code = $request->postal_code;
- $address->phone = $request->phone;
- $address->save();
- return response()->json([
- 'result' => true,
- 'message' => translate('Shipping information has been updated successfully')
- ]);
- }
- public function updateShippingAddressLocation(Request $request)
- {
- $address = Address::find($request->id);
- $address->latitude = $request->latitude;
- $address->longitude = $request->longitude;
- $address->save();
- return response()->json([
- 'result' => true,
- 'message' => translate('Shipping location in map updated successfully')
- ]);
- }
- public function deleteShippingAddress($id)
- {
- $address = Address::where('id',$id)->where('user_id',auth()->user()->id)->first();
- if($address == null) {
- return response()->json([
- 'result' => false,
- 'message' => translate('Address not found')
- ]);
- }
- $address->delete();
- return response()->json([
- 'result' => true,
- 'message' => translate('Shipping information has been deleted')
- ]);
- }
- public function makeShippingAddressDefault(Request $request)
- {
- Address::where('user_id', auth()->user()->id)->update(['set_default' => 0]); //make all user addressed non default first
- $address = Address::find($request->id);
- $address->set_default = 1;
- $address->save();
- return response()->json([
- 'result' => true,
- 'message' => translate('Default shipping information has been updated')
- ]);
- }
- public function updateAddressInCart(Request $request)
- {
- try {
- Cart::where('user_id', auth()->user()->id)->update(['address_id' => $request->address_id]);
- } catch (\Exception $e) {
- return response()->json([
- 'result' => false,
- 'message' => translate('Could not save the address')
- ]);
- }
- return response()->json([
- 'result' => true,
- 'message' => translate('Address is saved')
- ]);
- }
- public function getCities()
- {
- return new CitiesCollection(City::where('status', 1)->get());
- }
- public function getStates()
- {
- return new StatesCollection(State::where('status', 1)->get());
- }
- public function getCountries(Request $request)
- {
- $country_query = Country::where('status', 1);
- if ($request->name != "" || $request->name != null) {
- $country_query->where('name', 'like', '%' . $request->name . '%');
- }
- $countries = $country_query->get();
-
- return new CountriesCollection($countries);
- }
- public function getCitiesByState($state_id,Request $request)
- {
- $city_query = City::where('status', 1)->where('state_id',$state_id);
- if ($request->name != "" || $request->name != null) {
- $city_query->where('name', 'like', '%' . $request->name . '%');
- }
- $cities = $city_query->get();
- return new CitiesCollection($cities);
- }
- public function getStatesByCountry($country_id,Request $request)
- {
- $state_query = State::where('status', 1)->where('country_id',$country_id);
- if ($request->name != "" || $request->name != null) {
- $state_query->where('name', 'like', '%' . $request->name . '%');
- }
- $states = $state_query->get();
- return new StatesCollection($states);
- }
- }
|