Hi i need to make this code clean to reduce performance from serve how can do that ? the code have request search, filter and export must change all code to have small code
Hi i need to make this code clean to reduce performance from serve how can do that ? the code have request search, filter and export must change all code to have small code
need to change this code to clean code with best performance in server
//request search
if ($request->filled('search')) {
// $users = User::search($request->search)->paginate(10);
$users = User::query()
->withCount('orders')
->where('id', 'Like', '%' . request('search') . '%')
->orwhere('first_name', 'Like', '%' . request('search') . '%')
->orwhere('last_name', 'Like', '%' . request('search') . '%')
->orwhere('mobile', 'Like', '%' . request('search') . '%')
->orwhere('email', 'Like', '%' . request('search') . '%')
->paginate(10);
//if have request search and need to check for export files and sort
if ($request->filled('filter') or $request->filled('export')) {
if ($request->filter == 0 and $request->export == 1) {
$users;
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('itsolutionstuff.pdf');
} elseif ($request->filter == 0 and $request->export == 2) {
$users;
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 0 and $request->export == 3) {
$users;
return (new UsersExport($users))->download('Alphabetical.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 0) {
$users;
}
if ($request->filter == 1 and $request->export == 1) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('first_name')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('itsolutionstuff.pdf');
} elseif ($request->filter == 1 and $request->export == 2) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('first_name')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 1 and $request->export == 3) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('first_name')->paginate(10);
return (new UsersExport($users))->download('Alphabetical.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 1) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('first_name')->paginate(10);
}
if ($request->filter == 2 and $request->export == 1) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('itsolutionstuff.pdf');
} elseif ($request->filter == 2 and $request->export == 2) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 2 and $request->export == 3) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->paginate(10);
return (new UsersExport($users))->download('Alphabetical.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 2) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->paginate(10);
}
if ($request->filter == 3 and $request->export == 1) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->orderBy('first_name')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('itsolutionstuff.pdf');
} elseif ($request->filter == 3 and $request->export == 2) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->orderBy('first_name')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 3 and $request->export == 3) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->orderBy('first_name')->paginate(10);
return (new UsersExport($users))->download('Alphabetical.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 3) {
$users = User::query()->withCount('orders')->where('id', 'Like', '%' . request('search') . '%')->orwhere('first_name', 'Like', '%' . request('search') . '%')->orwhere('last_name', 'Like', '%' . request('search') . '%')->orwhere('mobile', 'Like', '%' . request('search') . '%')->orwhere('email', 'Like', '%' . request('search') . '%')->orderBy('orders_count', 'desc')->orderBy('first_name')->paginate(10);
}
}
//if not have request search and need to make filter sort
} elseif ($request->filled('filter')) {
if ($request->filter == 1 and $request->export == 1) {
$users = User::withCount('orders')->orderBy('first_name')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('itsolutionstuff.pdf');
} elseif ($request->filter == 1 and $request->export == 2) {
$users = User::withCount('orders')->orderBy('first_name')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 1 and $request->export == 3) {
$users = User::withCount('orders')->orderBy('first_name')->paginate(10);
return (new UsersExport($users))->download('Alphabetical.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 1) {
$users = User::withCount('orders')->orderBy('first_name')->paginate(10);
} elseif ($request->filter == 2 and $request->export == 1) {
$users = User::withCount('orders')->orderBy('orders_count', 'desc')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('Most_orders.pdf');
} elseif ($request->filter == 2 and $request->export == 2) {
$users = User::withCount('orders')->orderBy('orders_count', 'desc')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 2 and $request->export == 3) {
$users = User::withCount('orders')->orderBy('orders_count', 'desc')->paginate(10);
return (new UsersExport($users))->download('invoices.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 2) {
$users = User::withCount('orders')->orderBy('orders_count', 'desc')->paginate(10);
} elseif ($request->filter == 3 and $request->export == 1) {
$users = User::withCount('orders')->orderBy('id', 'DESC')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('Date_of_registration.pdf');
} elseif ($request->filter == 3 and $request->export == 2) {
$users = User::withCount('orders')->orderBy('id', 'DESC')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 3 and $request->export == 3) {
$users = User::withCount('orders')->orderBy('id', 'DESC')->paginate(10);
return (new UsersExport($users))->download('invoices.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 3) {
$users = User::withCount('orders')->orderBy('id', 'DESC')->paginate(10);
} elseif ($request->filter == 0 and $request->export == 1) {
$users = User::withCount('orders')->paginate(10);
$pdf = Pdf::loadView('myPDF', compact('users'));
return $pdf->download('Date_of_registration.pdf');
} elseif ($request->filter == 0 and $request->export == 2) {
$users = User::withCount('orders')->paginate(10);
return Excel::download(new UsersExport($users), 'users-collection.xlsx');
} elseif ($request->filter == 0 and $request->export == 3) {
$users = User::withCount('orders')->paginate(10);
return (new UsersExport($users))->download('invoices.csv', \Maatwebsite\Excel\Excel::CSV);
} elseif ($request->filter == 0) {
$users = User::withCount('orders')->orderBy('id', 'DESC')->paginate(10);
}
} else {
//if not have any request search
$users = User::withCount('orders')->paginate(10);
}
return view('admin.users.index', compact('users'));
via
Chebli Mohamed