简体   繁体   中英

how can i get profit for today, last 7 days and last 30 days using laravel

在此处输入图像描述 I have a profit table in my database, when I retrieve data from database I need to filter profit according to date (for today , last 7 days , last 30 days ).

In my controller, I tried,

$todayProfit = Profit::whereDate('created_at',today())->get();
return response()->json($todayProfit);

but it provides only 24 hours, but I need today profit.

when i retrieve data to last 30 days using,

$lastMonthProfit = Profit::whereDate('created_at',today()->subDay(30))->get();
return response()->json($lastMonthProfit );

it is not working for me, how can I solve

The query for calculating all in a single one could be;

SELECT 'Today' as date, sum(profit) as total FROM profits 
WHERE created_at > DATE_ADD(CURDATE(), INTERVAL -1 DAY)
UNION
SELECT 'Last 7 days', sum(profit) FROM profits 
WHERE created_at > DATE_ADD(CURDATE(), INTERVAL -7 DAY)
UNION
SELECT 'Last 30 days', sum(profit) FROM profits 
WHERE created_at > DATE_ADD(CURDATE(), INTERVAL -30 DAY)
UNION
SELECT 'All', sum(profit) from profits;

The query builder version;

return Profit::where('created_at', '>', DB::raw('DATE_ADD(CURDATE(), INTERVAL -1 DAY)'))
        ->select([DB::raw('"Today" as date'), DB::raw('sum(profit) as total')])
        ->union(
            Profit::where('created_at', '>', DB::raw('DATE_ADD(CURDATE(), INTERVAL -7 DAY)'))
                ->select([DB::raw('"Last 7 days"'), DB::raw('sum(profit) as total')])
        )
        ->union(
            Profit::where('created_at', '>', DB::raw('DATE_ADD(CURDATE(), INTERVAL -30 DAY)'))
                ->select([DB::raw('"Last 30 days"'), DB::raw('sum(profit) as total')])
        )
        ->union(
            Profit::select([DB::raw('"All"'), DB::raw('sum(profit) as total')])
        )
        ->get();

it prints something like these;

[
    {
        "date": "Today",
        "total": "50"
    },
    {
        "date": "Last 7 days",
        "total": "145"
    },
    {
        "date": "Last 30 days",
        "total": "360"
    },
    {
        "date": "All",
        "total": "695"
    }
]

You are trying to get only data of 30 days ago not from 30 days ago till now. To solve that problem maybe you should use whereBetween . Try this and let me know if it solves your problem.

$todayProfit = Profit::whereBetween('created_at', [now()->startOfDay(), now()]);

$lastMonthProfit = Profit::whereBetween('created_at', [now()->subMonth(), now()]);

Try this code get data to last 30 days using,

$date = Carbon::today()->subDays(30);
$lastMonthProfit = Profit::where('created_at','>=',$date)->get();
return response()->json($lastMonthProfit );

last 7 days

$date = Carbon::today()->subDays(7);
$lastSevenDayProfit = Profit::where('created_at','>=',$date)->get();
return response()->json($lastSevenDayProfit );

today

$todayProfit = Profit::whereDate('created_at', Carbon::today())->get();
return response()->json($todayProfit,200);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM