我想请你帮忙。我不是一个严格的编码人员,所以我无法构建执行此操作的正确代码。
我有一个订单付款应用程序,可以保存某些订单的付款。那里没有问题。数据正在保存在 ORDER_PAYMENTs 表中。
在我的创建函数中,我有这个
public function create($order_id)
{
$order = Orders::find($order_id);
$order->company;
$order->currency;
$banks = Banks::all('name','acronym','id')->pluck('name','id')->all();
$balance = Orderpayments::where('order_id', '=', $order_id)->sum('amount');
// dd($balance);
return view('orderPayments.create', compact('balance'))
->with('banks', $banks)
->with('order', $order);
}
变量 $ balance 进行总金额的计算,然后在create.blade
中进行实际计算,以检查是否还有余额。这是它的样子
<div class="form-group">
<span><strong>Remaining Balance</strong></span>
<p class="text-danger">@php $remainingBalance = $order->grandtotal-$balance; @endphp
{{ number_format($remainingBalance, 2) }}</p>
</div>
现在,我要做的是根据存储数据(我指的是存储功能)做出一个条件,即如果付款涵盖了所有剩余余额,则应更新该列, ORDERS 表中的 PAID 。这是我的商店功能的当前外观。
public function store(Request $request)
{
$this->validate($request, [
'order_id' => '',
'company_id' => '',
'currency_id' => '',
'bank' => '',
'type' => '',
'payment_date' => '',
'amount' => 'min:1',
'rate' => '',
'bank' => '',
'page' => '',
'number' => '',
'booklet_number'=> '',
'voucher_num' => '',
'notes' => '',
'user_id' => ''
]);
$orderpayments = Orderpayments::create($request->only(
'order_id',
'company_id',
'currency_id',
'bank',
'type',
'payment_date',
'amount',
'rate',
'bank',
'page',
'number',
'booklet_number',
'voucher_num',
'notes',
'user_id'
));
// NO IDEA ON HOW TO MAKE THE CONDITION PROPERLY
// $balance = Orderpayments::where('order_id', '=', $order_id)->sum('amount');
$order = Orders::all();
// $paidOrder = $order->paid;
dd($balance);
// if ($balance < 1) {
// $amount = ;
// $item->save();
// }
if ($request){
Session::flash('message','Purchase order was successfully added');
Session::flash('m-class','alert-success');
} else {
Session::flash('message','Data is not saved');
Session::flash('m-class','alert-danger');
return redirect()->route('orders.index');
}
return redirect()->route('orders.index');
}
首先,我正在考虑从 CREATE 函数获取$ balance变量,因为它已经具有计算能力,然后从那里检查$balance
是否小于1,然后如果是将 PAID 列从 Orders表更新为“ 1”
我不是一个硬编码者,我之所以走得这么远,是因为来自各地的教程都在这里问。我找不到合适的教程来解决我的问题。您可以帮助我构建我想要实现的逻辑吗?
提前谢谢你们!
进度更新
我现在有这个
// HERE'S THE PART THAT THE CONDITION IS PLACE
//get the total amount paid
$totalPaidAmount = Orderpayments::where('order_id', '=', $orderpayments->order_id)->sum('amount');
//get the total order cost
$orderGrandTotal = Orders::where('id', '=', $orderpayments->order_id)->get();
//Set variable for grand total attribute Orders table
$grandtotal = $orderGrandTotal->first()->grandtotal;
//set variable for paid attribute in Orders table
$paid = $orderGrandTotal->first()->paid;
//Get the difference between total order cost and total amount paid
$totalBalance = $grandtotal - $totalPaidAmount;
// dd($totalBalance);
if ($totalBalance == 0) {
$paid = 1;
$paid->save();
}
我的新问题是,当余额达到0时,我输入的条件应保存$ paid = 1对吗?相反,我遇到了这个错误
目前,这就是我的问题。保存部分和条件。
我希望你能帮助我。在此先感谢!
已解决
以防万一,在这种情况下您会陷入自我陷阱,这就是我所做的。
// HERE'S THE PART THAT THE CONDITION IS PLACE
//get the total amount paid
$totalPaidAmount = Orderpayments::where('order_id', '=', $orderpayments->order_id)->sum('amount');
//get the total order cost
$orderGrandTotal = Orders::where('id', '=', $orderpayments->order_id)->get();
//Set variable for grand total attribute Orders table
$grandtotal = $orderGrandTotal->first()->grandtotal;
//Get the difference between total order cost and total amount paid
$totalBalance = $grandtotal - $totalPaidAmount;
if ($totalBalance < 1) {
$setToPaid = \App\Orders::where('id', '=', $orderpayments->order_id)->first();
$setToPaid->paid = 1;
$setToPaid->save();
}
if ($request){
Session::flash('message','Purchase order was successfully added');
Session::flash('m-class','alert-success');
} else {
Session::flash('message','Data is not saved');
Session::flash('m-class','alert-danger');
return redirect()->route('orders.index');
}
return redirect()->route('orders.index');
Learn more here感谢@Marco的提示!
答案 0 :(得分:0)
从docs
// HERE'S THE PART THAT THE CONDITION IS PLACE
//get the total amount paid
$totalPaidAmount = Orderpayments::where('order_id', '=', $orderpayments->order_id)->sum('amount');
//get the total order cost
//$orderGrandTotal = Orders::where('id', '=', $orderpayments->order_id)->get();
$orderGrandTotal = Orders::find($orderpayments->order_id);
//Set variable for grand total attribute Orders table
$grandtotal = $orderGrandTotal->grandtotal;
//set variable for paid attribute in Orders table
$paid = $orderGrandTotal->paid;
//Get the difference between total order cost and total amount paid
$totalBalance = $grandtotal - $totalPaidAmount;
// dd($totalBalance);
if ($totalBalance == 0) {
$orderGrandTotal->paid = 1;
$orderGrandTotal->save();
}
我还没有测试过,但这是您想要走的方向。