TaskA/app/Http/Controllers/backend/UserController.php
2024-06-21 13:36:17 +05:45

146 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use App\Mail\FirstMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Mail;
use Exception;
use Illuminate\Support\Facades\Log;
class UserController extends Controller
{
// public function __construct()
// {
// $this->middleware('auth');
// }
public function AllUser()
{
$all = DB::table('users')->get();
return view('backend.user.all-user', compact('all'));
}
public function index()
{
$users = DB::table('users')->paginate(10);
return view('backend.users.index', compact('users'));
}
public function AddUser()
{
return view('backend.user.add-user');
}
public function StoreUser(Request $request)
{
$data = [];
$data['name'] = $request->input('name');
$data['email'] = $request->input('email');
// $data['password'] = bcrypt($request->input('password'));
$data['password'] = Hash::make($request->input('password'));
$data['role'] = $request->input('role');
$data['created_at'] = now();
$data['updated_at'] = now();
$insert = DB::table('users')->insert($data);
if ($insert) {
$notification = array(
'message' => 'User Added Successfully',
'alert-type' => 'success'
);
return redirect()->route('alluser')->with($notification);
} else {
$notification = array(
'message' => 'User Added Unsuccessfully',
'alert-type' => 'error'
);
return redirect()->route('alluser')->with($notification);
}
}
public function EditUser($id)
{
$edit = DB::table('users')->where('id', $id)->first();
return view('backend.user.edit-user', compact('edit'));
}
public function UpdateUser(Request $request, $id)
{
$data = [];
$data['name'] = $request->input('name');
$data['email'] = $request->input('email');
if ($request->input('password')) {
$data['password'] = Hash::make($request->input('password'));
}
$data['role'] = $request->input('role');
$data['updated_at'] = now();
$update = DB::table('users')->where('id', $id)->update(
$data
);
if ($update) {
$notification = array(
'message' => 'User Updated Successfully',
'alert-type' => 'success'
);
return redirect()->route('alluser')->with($notification);
} else {
$notification = array(
'message' => 'User is not Updated',
'alert-type' => 'error'
);
return redirect()->route('alluser')->with($notification);
}
}
public function DeleteUser($id)
{
DB::table('users')->where('id', $id)->delete();
return response()->json(['status' => 200, 'message' => 'Deleted Successfully!']);
}
public function ShowEmailForm()
{
return view('backend.user.send-email-form');
}
public function SendEmail(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
]);
try {
Mail::to($request->input('email'))->send(new FirstMail($request->input('name')));
$notification = [
'message' => 'Email Sent Successfully',
'alert-type' => 'success',
];
return redirect()->route('show-email-form')->with($notification);
} catch (Exception $e) {
Log::error('Error sending email: ' . $e->getMessage());
$notification = [
'message' => 'An error occurred while sending the email.',
'alert-type' => 'error',
];
return redirect()->route('show-email-form')->with($notification);
}
}
}