| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
-
- namespace App\Services;
-
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
-
- class QueryService
- {
- public static function amount(
- string $table,
- string $title,
- string $description
- ) {
- return DB::table($table)
- ->selectRaw(
- "COUNT(*) AS amount, (
- SELECT
- COUNT(*)
- FROM
- products
- WHERE
- DATE(created_at) = CURDATE()) AS amountToday"
- )
- ->get()
- ->transform(
- fn($table) => [
- "title" => $title,
- "amount" => $table->amount,
- "amountToday" => $table->amountToday,
- "amountTodayDescription" => $description,
- ]
- )
- ->first();
- }
-
- public static function amountStatistic(string $table)
- {
- return DB::table($table)
- ->selectRaw(
- "COUNT(*) AS amount,
- DATE_FORMAT(created_at, '%b') AS month"
- )
- ->whereRaw("YEAR(created_at) = YEAR(CURDATE())")
- ->groupByRaw("month")
- ->orderByRaw("created_at")
- ->get()
- ->pluck("amount", "month");
- }
-
- public static function priceStatistic(string $table)
- {
- return DB::table($table)
- ->selectRaw(
- "price,
- created_at"
- )
- ->orderByRaw("created_at")
- ->get()
- ->groupBy([
- fn($value) => Carbon::parse($value->created_at)->format("Y"),
- fn($value) => Carbon::parse($value->created_at)->format("M"),
- ])
- ->take(2)
- ->transform(
- fn($year) => $year->transform(
- fn($month) => $month->sum(fn($arr) => $arr->price)
- )
- );
- }
- }
|