has('customer_id')){
$address->user_id = $request->customer_id;
}
else{
$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->longitude = $request->longitude;
$address->latitude = $request->latitude;
$address->postal_code = $request->postal_code;
$address->phone = $request->phone;
$address->save();
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$data['address_data'] = Address::findOrFail($id);
$data['states'] = State::where('status', 1)->where('country_id', $data['address_data']->country_id)->get();
$data['cities'] = City::where('status', 1)->where('state_id', $data['address_data']->state_id)->get();
$returnHTML = view('frontend.partials.address_edit_modal', $data)->render();
return response()->json(array('data' => $data, 'html'=>$returnHTML));
// return ;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$address = Address::findOrFail($id);
$address->address = $request->address;
$address->country_id = $request->country_id;
$address->state_id = $request->state_id;
$address->city_id = $request->city_id;
$address->longitude = $request->longitude;
$address->latitude = $request->latitude;
$address->postal_code = $request->postal_code;
$address->phone = $request->phone;
$address->save();
flash(translate('Address info updated successfully'))->success();
return back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$address = Address::findOrFail($id);
if(!$address->set_default){
$address->delete();
return back();
}
flash(translate('Default address can not be deleted'))->warning();
return back();
}
public function getStates(Request $request) {
$states = State::where('status', 1)->where('country_id', $request->country_id)->get();
$html = '';
foreach ($states as $state) {
$html .= '';
}
echo json_encode($html);
}
public function getCities(Request $request) {
$cities = City::where('status', 1)->where('state_id', $request->state_id)->get();
$html = '';
foreach ($cities as $row) {
$html .= '';
}
echo json_encode($html);
}
public function set_default($id){
foreach (Auth::user()->addresses as $key => $address) {
$address->set_default = 0;
$address->save();
}
$address = Address::findOrFail($id);
$address->set_default = 1;
$address->save();
return back();
}
}