how to group by week and display list of weeks in the blade file in laravel

Viewed 28

i have a table called transaction, and in the table i have 3 columns called Amount, Profit and Status. in my blade file i have a table where i'm displaying each week total amount , total profit and count the status of each week.

i am stranded cause i don't know how to implement this

this is my controller so far.

$transact = Transaction::select('amount', 'profit', 'status')->get()
    ->groupBy(function($row){
        return $row->created_at->format('W');
    });

and my blade

<tbody>
    @foreach ($transact as $key=> $dae)
    <tr>
    <td>this is meant to display the week but i don't know how to do it</td>
    <td>{{ $dae->count() }}</td>
    <td>this is meant to display the sum of the 'amount' for the week</td>
    <td>this is meant to display the sum of the 'profit' for the week </td>
    <td>this is meant to display the count of 'status' for the week</td>
    </tr>
    @endforeach
 </tbody>
1 Answers

You should make use of Carbon package. That is what can format your date.

First off at the top of your controller, add this: use Carbon\Carbon; to include the Carbon package.

Then modify your controller code to this:

$transact = Transaction::select('amount', 'profit', 'status')->get()
    ->groupBy(function($row){
        return Carbon::parse($row->created_at)->format('W');
    });
Related