[英]Optimize in_array function without using isset?
如何優化我的 function 中的 in_array?
我嘗試了isset,但它不起作用。
public function index() {
$this->checkPermission();
$banks = BankAccount::all();
$clients = Role::whereRoleSlug('client')
->firstOrFail()
->users;
$projects = Project::whereProjectStatus('active')->get();
$adminBanks = BankAccount::where('bank_user_id', '=', null)->get();
$roles = Role::whereIn('role_slug', ['administrator', 'accountant'])
->pluck('role_id')
->toArray();
$cash = 0;
$payments = Payment::wherePaymentBy('cash')->get();
/*->filter(function ($payment) {
return in_array($payment->activity->activityBy->id, $roles)
});*/
foreach ($payments as $index => $payment) {
if(in_array($payment->activity->activityBy->role_id, $roles)) {
if(in_array(strtolower($payment->payment_purpose), ['employee_transfer', 'employee_refund', 'vendor_payment', 'vendor_refund', 'loan_payment', 'salary', 'office_deposit'])) {
$cash -= $payment->payment_amount;
}
else {
$cash += $payment->payment_amount;
}
}
}
// foreach ($payments as $index => $payment) {
// if (isset($payment->activity->activityBy->role_id, $roles)) {
// if (isset($payment['employee_transfer'], $payment['employee_refund'], $payment['vendor_payment'], $payment['vendor_refund'], $payment['loan_payment'], $payment['salary'], $payment['office_deposit'])) {
// $cash -= $payment->payment_amount;
//// dd($roles);
// } else {
// $cash += $payment->payment_amount;
// }
// }
// }
return view('admin.accounting.banks.index')
->with([
'adminBanks' => $adminBanks,
'banks' => $banks,
'clients' => $clients,
'projects' => $projects,
'cash' => $cash
]);
}
public function bankDetails($id) {
if(!Auth::user()->isAdmin() && !Auth::user()->isAccountant()) {
return redirectBackWithNotification('error', 'You are not authorised!');
}
if($id == 'cash') {
$projects = Project::select(['bsoft_projects.project_id', 'bsoft_projects.project_name'])->get();
return view('admin.accounting.banks.show')
->with([
'projects' => $projects
]);
}
$bank = BankAccount::findOrFail($id);
if(!$bank->user) {
$payments = Payment::where('payment_from_bank_account', '=', $bank->bank_id)
->orWhere('payment_to_bank_account', '=', $bank->bank_id)
->get();
$balance = $bank->bank_balance;
}
else {
$payments = Payment::where('payment_from_bank_account', '=', $bank->bank_id)
->orWhere('payment_to_bank_account', '=', $bank->bank_id)
->orWhere('payment_to_user', '=', $bank->user->id)
->orWhere('payment_from_user', '=', $bank->user->id)
->get();
$balance = 0;
$exp = 0;
$inc = 0;
foreach ($payments as $payment) {
if($payment->payment_from_user == $bank->user->id) {
$exp += $payment->payment_amount;
}
elseif ($payment->payment_to_user == $bank->user->id) {
$inc += $payment->payment_amount;
}
}
$balance = $inc - $exp;
}
return view('admin.accounting.banks.show')
->with([
'bank' => $bank,
'payments' => $payments,
'balance' => $balance
]);
}
public function rechargeFromCustomer(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'user_id' => ['required', 'numeric'],
'bank_id' => ['nullable', 'numeric'],
'project_id' => ['required', 'numeric'],
'type' => ['required', 'string'],
'amount' => ['required', 'numeric'],
'date' => ['required', 'date'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$payment = createNewPayment([
'type' => 'credit',
'to_user' => null,
'from_user' => $request->post('user_id'),
'to_bank_account' => (strtolower($request->post('type')) === 'bank' || strtolower($request->post('type')) === 'check')
? $request->post('bank_id') : null,
'from_bank_account' => null,
'amount' => $request->post('amount'),
'project' => $request->post('project_id'),
'purpose' => 'project_money',
'by' => $request->post('type'),
'date' => $request->post('date'),
'image' => null,
'note' => $request->post('note')
]);
if(!$payment) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('type') == 'check') {
$offBank = BankAccount::findOrFail($request->post('bank_id'));
$offBank->bank_balance = $offBank->bank_balance + (float) $request->post('amount');
$offBank->save();
}
return redirectBackWithNotification('success', 'Client Money Successfully Received!');
}
public function storeAccount(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'user_id' => ['nullable', 'numeric'],
'name' => ['required', 'string'],
'number' => ['required', 'string'],
'bank' => ['required', 'string'],
'branch' => ['required', 'string'],
'balance' => ['required', 'numeric'],
'accountFor' => ['required', 'string'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$bank = new BankAccount();
$bank->bank_user_id = ($request->post('user_id')) ? $request->post('user_id') : null;
$bank->bank_account_name = $request->post('name');
$bank->bank_account_no = $request->post('number');
$bank->bank_name = $request->post('bank');
$bank->bank_branch = $request->post('branch');
$bank->bank_balance = $request->post('balance');
if(!$bank->save()) {
return redirectBackWithNotification();
}
addActivity('bank', $bank->bank_id, 'Bank Account Added');
return redirectBackWithNotification('success', 'Bank Account Successfully Added!');
}
public function transferToEmployee(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'employee_id' => ['required', 'string'],
'bank_id' => ['nullable', 'numeric'],
'project_id' => ['required', 'numeric'],
'type' => ['required', 'string'],
'amount' => ['required', 'numeric'],
'date' => ['required', 'date'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
if(strtolower($request->post('type')) !== 'cash' && $request->post('bank_id') === null) {
return redirectBackWithNotification('error', 'Bank Account must be selected!');
}
$employee_id = $request->post('employee_id');
$employee_bank_id = null;
if(strpos($request->post('employee_id'), '@') !== false) {
$employee_id = Str::before($request->post('employee_id'), '@');
$employee_bank_id = Str::after($request->post('employee_id'), '@');
}
$paymentData = [
'type' => null,
'to_user' => $employee_id,
'from_user' => Auth::id(),
'to_bank_account' => $employee_bank_id,
'from_bank_account' => (strtolower($request->post('type')) !== 'cash') ? $request->post('bank_id') : null,
'amount' => $request->post('amount'),
'project' => $request->post('project_id'),
'purpose' => 'employee_transfer',
'by' => strtolower($request->post('type')),
'date' => $request->post('date'),
'image' => null,
'note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('payment_by') == 'check') {
$officeBank = BankAccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance - (float) $request->post('amount');
$officeBank->save();
$employeeBank = BankAccount::findOrFail($employee_bank_id);
$employeeBank->bank_balance = $employeeBank->bank_balance + (float) $request->post('amount');
$employeeBank->save();
}
return redirectBackWithNotification('success', 'Transfer successfully made!');
}
public function transferToOffice(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'employee_id' => ['required', 'string'],
'bank_id' => ['nullable', 'numeric'],
'project_id' => ['required', 'numeric'],
'type' => ['required', 'string'],
'amount' => ['required', 'numeric'],
'date' => ['required', 'date'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
if(strtolower($request->post('type')) !== 'bank' && $request->post('bank_id') === null) {
return redirectBackWithNotification('error', 'Bank Account must be selected!');
}
$employee_id = $request->post('employee_id');
$employee_bank_id = null;
if(strpos($request->post('employee_id'), '@') !== false) {
$employee_id = Str::before($request->post('employee_id'), '@');
$employee_bank_id = Str::after($request->post('employee_id'), '@');
}
else {
$employee_bank_id = $this->createAutoGeneratedAccount($employee_id)->bank_id;
}
$paymentData = [
'type' => null,
'to_user' => $employee_id,
'from_user' => null,
'to_bank_account' => $employee_bank_id,
'from_bank_account' => (strtolower($request->post('type')) === 'bank') ? $request->post('bank_id') : null,
'amount' => $request->post('amount') - ($request->post('amount') * 2),
'project' => $request->post('project_id'),
'purpose' => 'employee_refund',
'by' => strtolower($request->post('type')),
'date' => $request->post('date'),
'image' => null,
'note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
if(strtolower($request->post('type')) === 'bank' || $request->post('payment_by') == 'check') {
$officeBank = BankAccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance + (float) $request->post('amount');
$officeBank->save();
}
$employeeBank = BankAccount::findOrFail($employee_bank_id);
$employeeBank->bank_balance = $employeeBank->bank_balance - (float) $request->post('amount');
$employeeBank->save();
return redirectBackWithNotification('success', 'Money successfully refunded!');
}
public function withdrawFromBank(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'bank_id' => ['nullable', 'numeric'],
'amount' => ['required', 'numeric'],
'date' => ['required', 'date'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$paymentData = [
'type' => null,
'to_user' => null,
'from_user' => null,
'to_bank_account' => null,
'from_bank_account' => $request->post('bank_id'),
'amount' => $request->post('amount'),
'project' => null,
'purpose' => 'office_withdraw',
'by' => 'cash',
'date' => $request->post('date'),
'image' => null,
'note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
$officeBank = BankAccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance - (float) $request->post('amount');
$officeBank->save();
return redirectBackWithNotification('success', 'Money successfully Withdrawn!');
}
public function depositToBank(Request $request) {
$this->checkPermission();
$validator = Validator::make($request->all(), [
'bank_id' => ['nullable', 'numeric'],
'amount' => ['required', 'numeric'],
'date' => ['required', 'date'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$paymentData = [
'type' => null,
'to_user' => null,
'from_user' => null,
'to_bank_account' => $request->post('bank_id'),
'from_bank_account' => null,
'amount' => $request->post('amount'),
'project' => null,
'purpose' => 'office_deposit',
'by' => 'cash',
'date' => $request->post('date'),
'image' => null,
'note' =>$request->post('note')
];
if(!createNewPayment($paymentData)) {
return redirectBackWithNotification();
}
$officeBank = BankAccount::findOrFail($request->post('bank_id'));
$officeBank->bank_balance = $officeBank->bank_balance + (float) $request->post('amount');
$officeBank->save();
return redirectBackWithNotification('success', 'Money successfully Deposited!');
}
public function income() {
$this->checkPermission();
$projects = Project::select(['bsoft_projects.project_id', 'bsoft_projects.project_name'])->get();
return view('admin.accounting.income')
->with([
'projects' => $projects
]);
}
public function expense() {
$this->checkPermission();
$projects = Project::select(['bsoft_projects.project_id', 'bsoft_projects.project_name'])->get();
return view('admin.accounting.expense')
->with([
'projects' => $projects
]);
}
public function getUsers(Request $request) {
$this->checkPermission();
$users = Role::whereRoleSlug($request->post('type'))->firstOrFail()
->users;
return view('admin.accounting.banks.ajax-users')
->with([
'users' => $users
]);
}
public function getClientProjects(Request $request) {
$this->checkPermission();
$projects = User::findOrFail($request->post('client_id'))->clientProjects()
->where('project_status', '=', 'active')->get();
return view('admin.accounting.banks.ajax-projects')
->with([
'projects' => $projects
]);
}
public function getManagers(Request $request) {
$this->checkPermission();
$roles = Role::whereIn('role_slug', ['manager'])
->pluck('role_id')
->toArray();
$users = Project::findOrFail($request->post('project_id'))->employees()
->whereIn('role_id', $roles)
->get();
return view('admin.accounting.banks.ajax-employees')
->with([
'users' => $users
]);
}
/**
* @return bool|\Illuminate\Http\RedirectResponse
*/
protected function checkPermission() {
$role = Auth::user()->role->role_slug;
if($role == 'administrator' || $role == 'accountant') {
return true;
}
return redirectBackWithNotification('error', 'Sorry! You Are Not Authorised!.');
}
protected function createAutoGeneratedAccount(int $id) {
$employee = User::findOrFail($id);
$bank = new BankAccount();
$bank->bank_account_name = 'Auto Generated Bank Account!';
$bank->bank_user_id = $employee->id;
$bank->save();
return $bank;
}
加載大約需要 4/5m,我在 localhost 中嘗試,但顯示 60 秒超時錯誤。我想在 30 秒內加載此頁面。 我無法在in_array($payment->activity->activityBy->role_id, $roles)
和
in_array(strtolower($payment->payment_purpose), ['employee_transfer', 'employee_refund', 'vendor_payment', 'vendor_refund', 'loan_payment', 'salary', 'office_deposit'])
主要是這兩個如果發生加載時間錯誤。 我可以優化 $payment 中的 in_array 嗎? $roles 中的 role_id 有什么問題? 為什么加載我的頁面需要這么長時間?
有什么方法可以優化 bankDetails function 因為加載 All、Loan 和 by Project 詳細信息也需要 3/4m?
如果您正在搜索數組中的鍵,我建議您使用array_key_exists
來檢查給定的鍵或索引是否存在於數組中(檢查鍵,而不是值)並返回真或假,因為 in_array 你檢查了value 存在於數組中(檢查值,而不是鍵)並返回 true 或 false。
您可以嘗試將更多過濾移至數據庫並減少加載所有模型的關系的需要:
$payments = Payment::wherePaymentBy('cash')
->whereHas('activity.activityBy', function ($query) use ($roles) {
$query->whereIn('role_id', $roles);
})->get();
然后你可以刪除if(in_array($payment->activity->activityBy->role_id, $roles))
。 這也應該消除動態加載所有這些模型(延遲加載)及其關系及其關系的關系的 N+1 問題。 還消除了必須為許多不需要的模型補水的任何開銷。
假設這是您正在努力獲得的一系列關系role_id
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.