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

like this


public function store(Request $request)
    {
        $this->validate($request,[
            'personal_id'=>'required|exists:customers,personal_id',
            'test_id'=>'required|exists:tests,id',
              'amount'=>'required|integer',
              'date'=>'required'
        ]);
        $ptest = Finance::create([
            'customer_id' => customer::where('personal_id', $request->personal_id)->first()->id,
            'test_id' => Test::findOrFail($request->id)->id,
            'date' => $request->date,
           'amount'=>$request->amount,
           'remaining'=>$request->amount-$request->test->payment,
           'note'=>$request->note
        ]);
0 likes
8 replies
Tray2's avatar

What is it that you are trying to do?

mohammadkhallaf's avatar

@Tray2 payment is a variable in other table i want to subtract it from variable amount and print the result in remaining

Tray2's avatar

@mohammadkhallaf I suggest handling that display in the front end for the display, and then update the table.

mubeensaeed's avatar

@mohammadkhallaf Get specific test first using $request->id, then use that test's payment $test = Test::findOrFail($request->id)->id; $payment = $test->payment;

mubeensaeed's avatar

@mohammadkhallaf Like this:

$test = Test::findOrFail($request->id);

$ptest = Finance::create([

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

Please or to participate in this conversation.