skip to Main Content

I want to display the total amount from start date and end date from tgl_trans, nasabah_id, jenis_id ??

$start = Carbon::parse($request->input('start'))->startOfDay();
$end = Carbon::parse($request->input('end'))->endOfDay();
$query = Transaksi::whereDate('tgl_trans', '>=', $start)->whereDate('tgl_trans', '<=', $end)->where('nasabah_id', $id)->get();

$kredit = $query->where('jenis_id', 1)->sum('nominal'); //result 0

Table transaksi

Schema::create('transaksis', function (Blueprint $table) {
$table->id();
$table->string('kode_trans');
$table->('nasabah_id');
$table->decimal('saldo_awal', $precision = 15, $scale = 2)->nullable();            
$table->unsignedBigInteger('jenis_id');
$table->foreign('jenis_id')->references('id')->on('jenis')->onDelete('cascade')->onUpdate('cascade');
$table->decimal('nominal', $precision = 15, $scale = 2)->nullable();
$table->decimal('saldo', $precision = 15, $scale = 2)->nullable();
$table->integer('diff_day')->nullable();
$table->unsignedBigInteger('user_id');
$table->date('tgl_trans');
$table->timestamps(); });

value on $kredit not 0

2

Answers


  1. Hard to debug the code, so I’ll leave a few suggestions:

    Make sure that the $id exists, is set, and is not null/zero (i.e. is valid).

    if (!$id){
        dd("ID is empty");
    }
    

    check if the query is empty

    (...)
    $query = Transaksi::whereDate('tgl_trans', '>=', $start)
             ->whereDate('tgl_trans', '<=', $end)
             ->where('nasabah_id', $id)
             ->get();
    
    if ($query->isEmpty()){
        dd("the result was empty");
    }}
    (...)    
    
    Login or Signup to reply.
  2. First of all your migration file at line no-3 columns data type should be

    $table->unsignedBigInteger('nasabah_id');
    

    Other all I tried myself was ok with TableData & output , you may try by entering menual data as flowing

            $tod = date("2022-05-15");
            $start = Carbon::parse($tod)->startOfDay();
            $end = Carbon::parse(now())->endOfDay();
            $id = 5;
    
            $kredit = Transaksi::whereDate('tgl_trans', '>=', $start)
                        ->whereDate('tgl_trans', '<=', $end)
                        ->where('nasabah_id', $id)
                        ->where('jenis_id', 10)
                        ->sum('nominal');
    
            return response($kredit);
        }
    

    If its ok, then you have to check you requested input fields else you may check your database data.
    Hope it will help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search