Как отобразить продажи продукта за месяц в Laravel - PullRequest
0 голосов
/ 22 мая 2019

Я чувствую трудности в этом разделе.поэтому я планирую отобразить данные о продажах продуктов A с первого месяца до текущего периода.где в каждом месяце будут отображаться данные о продажах.Однако здесь мне сложно отобразить данные о продажах.Кроме того, отображаемый месяц должен быть ограничен в этом месяце, поэтому ближайшие месяцы не появляются первыми.

Это мой код

Контроллер:

public function index($id)
    {
        $thisYears = date('Y');
        $thisMonth = date('m');

        $produk = Product::findOrFail($id);
        $transaksi = Transaction::where('product_id', $produk->id)
                     ->where('created_at', 'LIKE','%'.$thisYears.'%')
                     // ->Where('status','!=','Menunggu')
                     ->get();
        $totalTrans = $transaksi->count();


        /*Date*/
        $months = array(1 => 'Januari '.$thisYears, 2 => 'Februari '.$thisYears, 3 => 'Maret '.$thisYears, 4 => 'April '.$thisYears, 5 => 'Mei '.$thisYears, 6 => 'Juni '.$thisYears, 7 => 'Juli '.$thisYears, 8 => 'Agustus '.$thisYears, 9 => 'September '.$thisYears, 10 => 'Oktober '.$thisYears, 11 => 'November '.$thisYears, 12 => 'Desember '.$thisYears);
        $transposed = array_slice($months, date('n'), 12, true) + array_slice($months, date('n'), true);
        $last8 = array_reverse(array_slice($transposed, -8, 12, true), true);
        return view('admin.product.insight.index', compact('transaksi','totalTrans','thisMonth','months','transposed','last8'));
    }

JS:

https://pastebin.com/cT8VMSy8

image display product data sales

1 Ответ

2 голосов
/ 22 мая 2019

Как упоминалось выше, попробуйте поработать с Carbon. Код комментируется, чтобы объяснить, что делается.

$data = [];

// Circle trough all 12 months
for ($month = 1; $month <= 12; $month++) {
    // Create a Carbon object from the current year and the current month (equals 2019-01-01 00:00:00)
    $date = Carbon::create(date('Y'), $month);

    // Make a copy of the start date and move to the end of the month (e.g. 2019-01-31 23:59:59)
    $date_end = $date->copy()->endOfMonth();

    $transaksi = Transaction::where('product_id', $produk->id)
        // the creation date must be between the start of the month and the end of the month
        ->where('created_at', '>=', $date)
        ->where('created_at', '<=', $date_end)
        // ->Where('status','!=','Menunggu')
        ->count();

    // Save the count of transactions for the current month in the output array
    $data[$month] = $transaksi;
}

Это даст вам массив, который должен выглядеть следующим образом, в зависимости от ваших данных:

array(12) {
  [1]=>int(254)
  [2]=>int(564)
  [3]=>int(345)
  [4]=>int(675)
  [5]=>int(234)
  [6]=>int(123)
  [7]=>int(143)
  [8]=>int(676)
  [9]=>int(787)
  [10]=>int(567)
  [11]=>int(780)
  [12]=>int(898)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...