Home > database >  groupBy and sum view in laravel
groupBy and sum view in laravel

Time:10-31

This is my controller

public function index()
{
    $students = Student::all();
    $users = auth()->user();
    $savings = Saving::where('user_id', '=', $users->id)->get();
    $saving = Saving::groupBy('user_id')
        ->selectRaw('sum(paid) as sum, user_id')
        ->pluck('sum','user_id');
    dd($saving);

    return view('pages.dashboard.siswa.saving.index', [
        'saving' => $saving
    ],

     compact('students', 'savings'));
}

this is my dd

Illuminate\Support\Collection {#1353 ▼
  #items: array:2 [▼
    1 => 30.0
    2 => 90.0
  ]
  #escapeWhenCastingToString: false
}

and this is my blade view

@foreach ($savings as $saving)
    <tr>
        <td>{{ $saving->student->name }}</td>
        <td>Rp. {{ number_format($saving->paid, 2) }}</td>
        <td>Rp. {{ number_format($saving->sum('paid'), 2) }}</td>
        <td>
            <div >
            <a href="{{ route('dashboard.saving.edit', $saving->id) }}"><button  data-toggle="tooltip" data-placement="top" title="Edit">
            <i ></i>
            </button></a>
            <form  action= {{ route('dashboard.saving.destroy', $saving->id)  }} method="POST">
                 csrf
                 method('delete')
                 <button  data-toggle="tooltip" data-placement="top" title="Delete">
                 <i ></i>
                 </button>
             </form>
             </div>
        </td>
       </tr>
       <tr ></tr>
 endforeach

This is my schema

Schema::create('savings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('student_id');
            $table->foreignId('user_id');
            $table->date('date');
            $table->string('day');
            $table->string('paid');
            $table->timestamps();
        });

I was try to sum the paid column, I can sum for all user. What I want to know is how can I only show the paid value with column user_id? Like when user_id = 1 access it, only show paid value for user_id = 1, not for the others. Thanks

CodePudding user response:

I was try to sum the paid column, I can sum for all user. What I want to know is how can I only show the paid value with column user_id? Like when user_id = 1 access it, only show paid value for user_id = 1, not for the others. Thanks

From what I understand, you want to get the paid records for specific users and sum it up.

In your controller

public function index() {
    $totalSavings = auth()->user()->savings()->sum('paid');
    // or
    $userId = 1;
    $totalSavings = User::where('user_id', $userId)->savings()->sum('paid');
    // or
    $totalSavings = Savings::where('user_id', $userId)->sum('paid');

    return view('pages.dashboard.siswa.saving.index', [
        'totalSavings ' => $totalSavings 
    ],
}

in your User eloquent model

// User.php

public function savings() {
    return $this->hasMany(Savings::class);
}

in your Savings eloquent model

// Savings.php

public function user() {
    return $this->belongsTo(User::class);
}

and in your blade view

<h1>{{ $totalSavings }}</h1>

But if you want to sum all paid records and show it beside every user, do this.

In your controller

public function index() {
    $savings = Savings::with('user')->withSum('savings', 'paid')->get();

    return view('pages.dashboard.siswa.saving.index', [
        'savings ' => $savings
    ],
}

in your User eloquent model

// User.php

public function savings() {
    return $this->hasMany(Savings::class);
}

in your Savings eloquent model

// Savings.php

public function user() {
    return $this->belongsTo(User::class);
}

and in your blade view

@forelse ($savings as $saving)
    <tr>
        <td>{{ $saving->user->name }}</td>
        <td>{{ $saving->savings_sum_paid }}</td>
    </tr>
@empty
@endforelse

NOTE: You need to use an integer in your paid column.

// Change this line
$table->string('paid');

// To
$table->integer('paid');

CodePudding user response:

I'm not sure what you want, but I think you want to get sum paid by specific user.

Try this code.

$savings = Saving::where('user_id', $users->id)->sum('paid');

or

$savings = Saving::selectRaw('sum(paid) as sum')
    ->where('user_id', '=', $users->id)
    ->first()->sum;
  • Related