Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohammadkhallaf's avatar

how can i subtract a variable from variable exists in other table

i want the remaining value be the value of $amount - the value of $payment

 public function store(Request $request)
    {
        $this->validate($request, [

            'customer_id' => 'required|exists:customers,personal_id',
            'test_id' => 'required|exists:tests,id',
            'amount' => 'required|integer',
            'date' => 'required'
        ]);

        $finances = Finance::create([
            'customer_id' => customer::where('personal_id', $request->personal_id)->first()->id,
            'test_id' => $request->test_id,
            'date' => $request->date,
            'amount' => $request->amount,
            'remaining' =>  $request->id ,
            'note' => $request->note ,
            $test = Test::findOrFail($request->id)->id,
              $payment = $test->payment
        ]);

        return redirect()->back()->with('success', 'تم اضافة معلومات مالية جديدة');

    }

 public function store(Request $request)
    {
        $this->validate($request, [
            'finance_id' => "required|exists:finances,id",
            'payment' => "required|integer",
            'date' => "required",

        ]);
        $transaction = Transaction::create([
            'finance_id'=> $request->finance_id,
            'payment' => $request->payment,
            'date' => $request->date,
            'note' => $request->note
        ]);
        return redirect()->route('admin.transaction')->with('success', 'تم اضافة دفعة جديدة بنجاح');
    }
0 likes
8 replies
tykus's avatar

I don't understand the problem; you have a Finance model and a Transaction model; but these are two different resources? Where is the computed value being derived from and where is it to be stored.

Looking at the code; it seems we're going to have all of the problems from the last thread again...

  • $request->id is not a thing...
  • you don't need this query $test = Test::findOrFail($request->id)->id; and its in the wrong place,
  • $test is not an Object $payment = $test->payment
mohammadkhallaf's avatar

@tykus

public function store(Request $request)
    {
        $this->validate($request, [

            'customer_id' => 'required|exists:customers,personal_id',
            'test_id' => 'required|exists:tests,id',
            'amount' => 'required|integer',
            'date' => 'required'
        ]);

        $finances = Finance::create([
            'customer_id' => customer::where('personal_id', $request->personal_id)->first()->id,
            'test_id' => $request->test_id,
            'date' => $request->date,
            'amount' => $request->amount,
            'remaining' =>  $request->id ,
            'note' => $request->note ,

        ]);

        return redirect()->back()->with('success', 'تم اضافة معلومات مالية جديدة');

    }
tykus's avatar
tykus
Best Answer
Level 104

@mohammadkhallaf so I am going to assume you are wanting to update the Finance Model remaining property whenever there is a new associated Transaction model created? Because whenever you create the Finance model the remaining amount should be the same as the amount; right? There is not Transaction in scope in that method!

So what we're doing here is (within a database transaction); creating the Transaction record as before, and then decrementing the Finance record by the payment amount:

 public function store(Request $request)
    {
        $this->validate($request, [
            'finance_id' => "required|exists:finances,id",
            'payment' => "required|integer",
            'date' => "required",

        ]);
        \DB::transaction(function () use ($request) {
            $transaction = Transaction::create([
                'finance_id'=> $request->finance_id,
                'payment' => $request->payment,
                'date' => $request->date,
                'note' => $request->note
            ]);
            Finance::where('id', $request->finance_id)
                ->decrement('remaining', $request->payment);
        });

        return redirect()->route('admin.transaction')->with('success', 'تم اضافة دفعة جديدة بنجاح');
    }

Please or to participate in this conversation.