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

mohammadkhallaf's avatar

why the decrement not work

i want to decremnt the $amount by $payment and the result appear in the feild called remaining $amount exist in other model called finance and this is its controller

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->customer_id)->first()->id,
            'test_id' => $request->test_id,
            'date' => $request->date,
            'amount' => $request->amount,
           // 'remaining' =>  $request->id ,
            'note' => $request->note ,

        ]);

this is other controller called TransactionContralloer

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', 'تم اضافة دفعة جديدة بنجاح');
    }
0 likes
30 replies
mohammadkhallaf's avatar

@Sinnbeck https://drive.google.com/file/d/1dBMWMChtY4jUNYWQkc7P1zLzIsXxzmil/view?usp=sharing

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('finances', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->foreign('customer_id')->references('id')->on('customers')->onUpdate('cascade')
            ->onDelete('cascade');
            $table->unsignedBigInteger('test_id');
            $table->foreign('test_id')->references('id')->on('tests')->onUpdate('cascade')
            ->onDelete('cascade');
            // $table->integer('year');
            // $table->integer('month');
            // $table->integer('day');
            $table->integer('amount');
            $table->integer('remaining');
            $table->date('date');
            $table->text('note')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('finances');
    }
};
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('finances', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->foreign('customer_id')->references('id')->on('customers')->onUpdate('cascade')
            ->onDelete('cascade');
            $table->unsignedBigInteger('test_id');
            $table->foreign('test_id')->references('id')->on('tests')->onUpdate('cascade')
            ->onDelete('cascade');
            // $table->integer('year');
            // $table->integer('month');
            // $table->integer('day');
            $table->integer('amount');
            $table->integer('remaining');
            $table->date('date');
            $table->text('note')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('finances');
    }
};


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('finance_id');
            $table->foreign('finance_id')->references('id')->on('finances')->onUpdate('cascade')
            ->onDelete('cascade');
            $table->integer('payment');
            $table->date('date');
            $table->text('note')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('transactions');
    }
};


Sinnbeck's avatar

@mohammadkhallaf yes I get wjats meant to happen. I'm interested what actually happens. What are in the variables. How the table row looks before and after the code is run

mohammadkhallaf's avatar

@Sinnbeck


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Finance extends Model
{
    use HasFactory;
    //protected $table = 'finance';
    protected $fillable = [
        'id',
        'customer_id',
        'test_id',
        'date',
        'amount',
        'remaining',
        'note',
        'created_at',
        'updated_at'
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function transaction()
    {
        return $this->hasMany(Transaction::class);
    }
}

mohammadkhallaf's avatar

@Sinnbeck the $remaining doesn't inserted in the form first time it should equal the$amountthen it decremnt by $payment after every payment operation

mohammadkhallaf's avatar

@Sinnbeck when i use this

$remaining=  Finance::where('id', $request->finance_id) ->decrement('amount', $request->payment);

the result appear in $amount feild i want it appear in $remaining feild

mohammadkhallaf's avatar

@Sinnbeck how to get thw result in the $remaining i want the result appear in the $remaining and the $amount doesnt change

Sinnbeck's avatar

@mohammadkhallaf maybe we are not understanding each other completely. If I visit the first controller with amount 500, what would the values of amount and remaining be. And if I visit the second controller with amount 200, what are the values in the database now?

Sinnbeck's avatar
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->customer_id)->first()->id,
            'test_id' => $request->test_id,
            'date' => $request->date,
            'amount' => $request->amount,
           /ø 'remaining' =>  $request->amount,
            'note' => $request->note ,

        ]);
 //and
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.