V
V
Vladimir Borisyuk2020-08-22 10:11:27
Laravel
Vladimir Borisyuk, 2020-08-22 10:11:27

How to extract data from the database for the current month and year + for the same month last year using Laravel eloquement?

How to extract data from the database for the current month of the current year + the analogous month of the last year using laravel eloquenent and correctly provide all this to the frontend written in vue.js?
The table has the following fields
id, value, date
For example, we extract data for August 2020 and we also need to extract data for August 2019.

On the frontend it will look like this:
| 08-2020 | 08-2019|

In the fields 08-2020 and 08-2019, you must write the value value from the database.
There can be n entries.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-08-22
@Flight404

Article::whereYear('created_at', now()->year)
       ->whereMonth('created_at', now()->month)
       ->orWhere(function ($query) {
           $query->whereYear('created_at', now()->subYear()->year)
                 ->whereMonth('created_at', now()->month);
          })
       ->get()
       ->groupBy(function($article) {
           return Carbon::parse($article->created_at)
                        ->format('m-Y');
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question