简体   繁体   中英

Update field from different Model after storing data to another table in Laravel

I would like to ask for your assistance. I am not a hard coder person so I can't construct the proper code of doing this.

I have an order payment app that saves the payment for certain orders. There are no issues there. the data is being save in ORDER_PAYMENTs table.

in my create function I have this

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);
}

the variable $balance do the computation of total amount then I will do the actual computation in create.blade to check if there's still a balance amount. here's what it looks like

    <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>  

Now, what I want to do is to make a condition upon STORING the data (I am referring to STORE function) that if the payment covers all the remaining balance, it should update the column, PAID in ORDERS table. Here's the current look of my store function.

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');
    }

At first, I am thinking to get the $balance variable from CREATE function since it is already has the computation then from there i'll check if the $balance is below 1 then if it is update the PAID column from Orders table to '1'

I am not a hard coder I only made this far because of the tutorials from everywhere and asking questions here. I can't find a proper tutorial to cover my problem. can you help me construct the logic that I want to achieve?

Thank you so much guys in advance!!!

PROGRESS UPDATE

I now have this

    // 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();    
    }

My new problem is that when the balance reach 0, the condition I put should save the $paid = 1 right? instead I am having this error

在此处输入图片说明

currently that is my problem now. the saving part and the condition.

I hope you could help me guys. thanks in advance!!!

SOLVED

guys just in case you are get your self trap in this kind of situation, here's what I did it.

    // 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 Thanks @Marco for the tip!

From the 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();    
}

I haven't tested but this is the direction you want need to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM