/home/awneajlw/.trash/app.1/Http/Controllers/Admin/UserController.php
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Branch;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;
use App\Http\Controllers\Controller;
use App\Models\UserDetail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
public function index()
{
$users = User::count();
$branches = Branch::all();
return view('admin.user.index',compact('users','branches'));
}
// yajra pagination
public function getUsers(Request $request)
{
if ($request->ajax()) {
$fromDate = $request->input('from_date');
$toDate = $request->input('to_date');
if($fromDate == null && $toDate == null){
$data = User::with('branch')->get();
}
else{
//Date Filter
$data = User::with('branch')->whereBetween('created_at', [$fromDate, $toDate])->get();
}
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function ($row) {
$btn = '<button class="btn btn-sm btn-info ml-2 my-3 edit" value="' . $row->id . '" type="button"><i class="fa fa-edit"></i></button>';
$btn = $btn . '<a class="btn btn-sm btn-danger ml-2 my-3" href="'.url('/user-delete/'.$row->id).'" role="button"><i class="fa fa-trash"></i></a>';
return $btn;
})
->addColumn('created', function ($row) {
$created_at = $row->created_at;
$formatted_date = Carbon::parse($created_at)->format('Y-m-d H:i:s');
return $formatted_date;
})
->rawColumns(['action', 'created'])
->make(true);
}
}
public function addUser(Request $req){
$password = $req->user_password;
$hashedPassword = Hash::make($password);
User::insert([
'name' => $req->user_name,
'email' => $req->user_email,
'password' => $hashedPassword,
'branch_id' => $req->branch_id,
'status' => $req->user_status,
'role_id' => 2,
'created_at' => now(),
]);
return redirect()->back();
}
// edit
public function editUser($id){
$user = User::find($id);
return response()->json([
'user'=>$user,
]);
}
// update user
public function updateUser(Request $req){
$user = User::find($req->userId);
$user->name = $req->name;
$user->email = $req->email;
$user->status = $req->status ?? 'active';
$user->branch_id = $req->branch_id ?? Auth::user()->branch_id;
$user->role_id = '2' ?? Auth::user()->role_id;
if($req->password != null){
$password = $req->password;
$hashedPassword = Hash::make($password);
$user->password= $hashedPassword;
}
$user->update();
return redirect()->back();
}
// delete user
public function deleteUser($id){
$user=User::find($id);
if ($user) {
$user->delete();
}
session()->flash('message', 'User deleted successfully!');
return redirect()->back();
}
// update profile
public function profileUser(){
$user = Auth::user();
$userDetail = UserDetail::where('user_id', $user->id)->first();
return view('admin.user.profile',compact('user','userDetail'));
}
// public function userDetail(Request $req){
// $userDetail = UserDetail::find($req->userId);
// $userDetailLogoPath = null;
// if ($req->hasFile('brand_logo')) {
// $destinationPath = public_path('userDetailLogo');
// $filename = time() . '_' . $req->file('brand_logo')->getClientOriginalName();
// $req->file('brand_logo')->move($destinationPath, $filename);
// $userDetailLogoPath = 'setting/' . $filename;
// }
// if($userDetail){
// $userDetail->brand_name = $req->brand_name;
// $userDetail->brand_detail = $req->brand_detail;
// $userDetail->brand_logo = $userDetailLogoPath;
// $userDetail->address = $req->address;
// $userDetail->country = $req->country;
// $userDetail->vat_no = $req->vat_no;
// $userDetail->company_ltd = $req->company_ltd;
// $userDetail->footer_no_email = $req->footer_no_email;
// $userDetail->footer_iban = $req->footer_iban;
// $userDetail->payment_reference = $req->payment_reference;
// $userDetail->update();
// }else{
// UserDetail::insert([
// 'brand_name' => $req->brand_name,
// 'brand_detail' => $req->brand_detail,
// 'brand_logo' => $userDetailLogoPath,
// 'address' => $req->address,
// 'country' => $req->country,
// 'vat_no' => $req->vat_no,
// 'company_ltd' => $req->company_ltd,
// 'footer_no_email' => $req->footer_no_email,
// 'footer_iban' => $req->footer_iban,
// 'payment_reference' => $req->payment_reference,
// 'created_at' => now(),
// ]);
// }
// return redirect()->back();
// }
public function userDetail(Request $req)
{
$validatedData = $req->validate([
'brand_name' => 'required|string|max:255',
'brand_detail' => 'required|string|max:255',
'brand_logo' => 'nullable|file|image|max:2048',
'address' => 'required|string|max:255',
'country' => 'required|string|max:255',
'vat_no' => 'required|string|max:255',
'company_ltd' => 'required|string|max:255',
'footer_no_email' => 'required|string|max:255',
'footer_iban' => 'required|string|max:255',
'payment_reference' => 'required|string|max:255',
]);
$userDetail = UserDetail::where('user_id', $req->userId)->first();
$user_branch_id = Auth::user()->branch_id;
$userDetailLogoPath = $this->uploadLogo($req);
$dataToUpdate = [
'branch_id' => $user_branch_id,
'brand_name' => $validatedData['brand_name'],
'brand_detail' => $validatedData['brand_detail'],
'address' => $validatedData['address'],
'country' => $validatedData['country'],
'vat_no' => $validatedData['vat_no'],
'company_ltd' => $validatedData['company_ltd'],
'footer_no_email' => $validatedData['footer_no_email'],
'footer_iban' => $validatedData['footer_iban'],
'payment_reference' => $validatedData['payment_reference'],
];
if ($userDetailLogoPath) {
$dataToUpdate['brand_logo'] = $userDetailLogoPath;
}
UserDetail::updateOrCreate(
['user_id' => $req->userId],
$dataToUpdate
);
return redirect()->back();
}
private function uploadLogo(Request $req)
{
if ($req->hasFile('brand_logo')) {
$destinationPath = public_path('userDetailLogo');
$filename = time() . '_' . $req->file('brand_logo')->getClientOriginalName();
$req->file('brand_logo')->move($destinationPath, $filename);
return 'userDetailLogo/' . $filename;
}
return null;
}
}