Group by month or week with Laravel eloquent

Asked

Viewed 452 times

0

I have a project that has managed my investments, that is, it takes my daily statement and saved in my project so that I can see how much it is yielding, in short I have a table that are daily these values, I can already generate the information separately per day and it works perfectly (in fact it is a little more complex than that what I do but the basis is this);

However now it is getting bad to visualize when I caught a date range very large for example 1 year, it lists every day, I wanted a way to be able to group by month or week,

Thanks in advance for any help.

        $rows =  AtivosExtrato::select('titulo_id', DB::raw('SUM(valor_bruto_atual) AS valor'), 'data_imports.data_import as created_at')
        ->join('titulos','titulo_id', '=', 'titulos.id' )
        ->join('representantes','representante_id', '=', 'representantes.id' )
        ->join('data_imports','data_import_id', '=', 'data_imports.id' )
        ->where('user_id', Auth::user()->id) 
        ->whereBetween('data_imports.data_import', [$request->input('start_date'), $request->input('end_date')])
        ->groupBy('titulos.nome_titulo')
        ->groupBy('data_imports.data_import')
        ->orderBy('data_import')
        ->orderBy('titulos.nome_titulo')
        ->get();   

Briefly explaining what each eloquent information is:

Ativosextrate: the model where the daily income information is located;

1) Join: foreign table for securities names

2) Join: foreign table for the broker name

3) John: table that saves the date of import and relates to the id in the table of activesExtrato, it has function to reduce the weight at the time of the searches and gain performance.

Where: limiting the user in question

Wherebetween: limiting to the date range

1) groupBy: Grouping by titles

2) groupBy: grouping by the date of import

  • 1

    In place of the date can not put Month of it? example ->groupBy(DB::raw('month(data_imports.data_import)')) and at Select that’s all I think about

  • Yes I tested it now, it still displays all, logic thinking well [and that instead of grouping it take the value of the last day of the month,

  • Your doubt is in doubt, you must always pose the problem ...

  • not necessarily I just cogited an idea, I just want a way to get the value grouped by week or month,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.