Level 16
change your loop to this
foreach ($users as $user) {...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I don't figure out the problem.
$users = User::get();
foreach ($users as $user => $value) {
if($user->hasBusinessLoans() == $user_id) {
$loan = BusinessLoan::where('user->id', $user->id)->where('completed', 0)->first();
$loan_installments = LoanInstallment::where('loan_id', $loan->id)->latest()->first();
$to = \Carbon\Carbon::createFromFormat($loan->approved_date);
$from = \Carbon\Carbon::createFromFormat($loan_installments->this_month);
$net_installment_month = $to->diffInMonths($from);
if($loan_installments == '') {
// user doesn't give first installment
$saving = SavingAcount::where('user_id', $user->id)->latest()->first();
$saving->total = $saving->total - 20;
$saving->save();
$username = "Alauddin101";
$hash = "4f9ec55ab0531a44a466910119d97847";
$numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
$message = '20tk has been deducted for your late of Loan from saving. Thank you! Your current saving is '.$saving->total;
$params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);
} elseif($loan_installments->amount < $net_installment_month * $loan->perInstallmentAmount) {
$saving = SavingAcount::where('user_id', $user->id)->latest()->first();
$saving->total = $saving->total - 20;
$saving->save();
$username = "Alauddin101";
$hash = "4f9ec55ab0531a44a466910119d97847";
$numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
$message = '20tk has been deducted for less amount then per installment amount of Loan from saving. Thank you! Your current saving is '.$saving->total;
$params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);
} else {
return 0;
}
}
Model
public function businessLoans() {
return $this->hasMany('App\BusinessLoan');
}
public function hasBusinessLoans() {
return $this->businessLoans->flatten()->pluck('user_id')->last();
}
They say erorr is in this line
if($user->hasBusinessLoans() == $user_id) {
But it outputs
dd($user->hasBusinessLoans()) =3
``
@rafidahsan you can really make your code much DRYER if you extract some methods.
$users = User::get();
$username = "Alauddin101";
$hash = "4f9ec55ab0531a44a466910119d97847";
/**
* @param string $username
* @param string $hash
* @param $numbers
* @param string $message
* @return array
*/
function sendMessage(string $username, string $hash, $numbers, string $message): array
{
$params = array(
'app' => 'ws',
'u' => $username,
'h' => $hash,
'op' => 'pv',
'unicode' => '1',
'to' => $numbers,
'msg' => $message
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?" . http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
/**
* @param $user
* @return mixed
*/
function decreaseSavings($user)
{
$saving = SavingAcount::where('user_id', $user->id)->latest()->first();
$saving->total = $saving->total - 20;
$saving->save();
return $saving;
}
foreach ($users as $user) {
if ($user->hasBusinessLoans() == $user->id) {
$loan = BusinessLoan::where('user_id', $user->id)->where('completed', 0)->first();
$loan_installments = LoanInstallment::where('loan_id', $loan->id)->latest()->first();
$to = \Carbon\Carbon::createFromFormat($loan->approved_date);
$from = \Carbon\Carbon::createFromFormat($loan_installments->this_month);
$net_installment_month = $to->diffInMonths($from);
$saving = decreaseSavings($user);
$numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
if ($loan_installments == '') {
// user doesn't give first installment
$message = '20tk has been deducted for your late of Loan from saving. Thank you! Your current saving is ' . $saving->total;
sendMessage($username, $hash, $numbers, $message);
} elseif ($loan_installments->amount < $net_installment_month * $loan->perInstallmentAmount) {
$message = '20tk has been deducted for less amount then per installment amount of Loan from saving. Thank you! Your current saving is ' . $saving->total;
sendMessage($username, $hash, $numbers, $message);
} else {
return 0; // Do you want to return here? first user who doesn't have a business loan will cause this method to exit. You probably should just continue or remove the return completely.
}
}
}
if you make your
$user->hasBusinessLoans()
return a Boolean, you can change
if($user->hasBusinessLoans() == $user->id)
to
if($user->hasBusinessLoans())
Please or to participate in this conversation.