updated to laravel 11
This commit is contained in:
135
app/Http/Controllers/AccountController.php
Normal file
135
app/Http/Controllers/AccountController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers;
|
||||
use App\Jobs\SendEmail;
|
||||
use App\Mail\EmailUpdateLink;
|
||||
use App\Mail\RegisterLink;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class AccountController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(User $user)
|
||||
{
|
||||
return view('account', compact('user'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'firstname' => 'required',
|
||||
'surname' => 'required',
|
||||
'email' => 'required|email',
|
||||
'phone' => 'required',
|
||||
|
||||
'home_address' => 'required_with:home_city,home_postcode,home_country,home_state',
|
||||
'home_city' => 'required_with:home_address,home_postcode,home_country,home_state',
|
||||
'home_postcode' => 'required_with:home_address,home_city,home_country,home_state',
|
||||
'home_country' => 'required_with:home_address,home_city,home_postcode,home_state',
|
||||
'home_state' => 'required_with:home_address,home_city,home_postcode,home_country',
|
||||
|
||||
'billing_address' => 'required_with:billing_city,billing_postcode,billing_country,billing_state',
|
||||
'billing_city' => 'required_with:billing_address,billing_postcode,billing_country,billing_state',
|
||||
'billing_postcode' => 'required_with:billing_address,billing_city,billing_country,billing_state',
|
||||
'billing_country' => 'required_with:billing_address,billing_city,billing_postcode,billing_state',
|
||||
'billing_state' => 'required_with:billing_address,billing_city,billing_postcode,billing_country',
|
||||
], [
|
||||
'firstname.required' => __('validation.custom_messages.firstname_required'),
|
||||
'surname.required' => __('validation.custom_messages.surname_required'),
|
||||
'email.required' => __('validation.custom_messages.email_required'),
|
||||
'email.email' => __('validation.custom_messages.email_invalid'),
|
||||
'phone.required' => __('validation.custom_messages.phone_required'),
|
||||
|
||||
'home_address.required' => __('validation.custom_messages.home_address_required'),
|
||||
'home_city.required' => __('validation.custom_messages.home_city_required'),
|
||||
'home_postcode.required' => __('validation.custom_messages.home_postcode_required'),
|
||||
'home_country.required' => __('validation.custom_messages.home_country_required'),
|
||||
'home_state.required' => __('validation.custom_messages.home_state_required'),
|
||||
|
||||
'billing_address.required' => __('validation.custom_messages.billing_address_required'),
|
||||
'billing_city.required' => __('validation.custom_messages.billing_city_required'),
|
||||
'billing_postcode.required' => __('validation.custom_messages.billing_postcode_required'),
|
||||
'billing_country.required' => __('validation.custom_messages.billing_country_required'),
|
||||
'billing_state.required' => __('validation.custom_messages.billing_state_required'),
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$userData = $request->all();
|
||||
|
||||
$newEmail = $userData['email'];
|
||||
unset($userData['email']);
|
||||
|
||||
if ($user->email !== $newEmail) {
|
||||
if(User::where('email', $request->get('email'))->exists()) {
|
||||
$validator->errors()->add('email', __('validation.custom_messages.email_exists'));
|
||||
return redirect()->back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
$token = Str::random(60);
|
||||
$user->emailUpdate()->delete();
|
||||
$emailUpdate = $user->emailUpdate()->create([
|
||||
'email' => $newEmail,
|
||||
'token' => $token
|
||||
]);
|
||||
|
||||
dispatch(new SendEmail($user->email, new EmailUpdateLink($token, $user->getName(), $user->email, $newEmail)))->onQueue('mail');
|
||||
}
|
||||
|
||||
$userData['subscribed'] = ($request->get('subscribed', false) === 'on');
|
||||
$user->update($userData);
|
||||
$user->save();
|
||||
|
||||
session()->flash('message', 'Your account details have been saved');
|
||||
session()->flash('message-title', 'Details updated');
|
||||
session()->flash('message-type', 'success');
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
auth()->logout();
|
||||
|
||||
$user->delete();
|
||||
|
||||
session()->flash('message', 'Your account has been deleted');
|
||||
session()->flash('message-title', 'Account Deleted');
|
||||
session()->flash('message-type', 'success');
|
||||
return redirect()->route('index');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user