【发布时间】:2021-10-23 02:10:29
【问题描述】:
这是我的工作。
当我运行此作业时,在线发票查询不显示任何内容,但有趣的是,当我交换 $onlineInvoices 和 $offlineInvoices 变量的位置时,$onlineInvoices 查询有效,而 $offlineInvoices 没有吨。
当我在$offline_sales 变量下方再次重新定义$monthlyInvoices 时,它工作正常,但我想知道为什么会发生这种情况。
<?php
namespace App\Jobs;
use App\Models\Invoice;
use App\Models\InvoiceMonthlyStatistics;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class InvoiceMonthlyStatisticsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$period = CarbonPeriod::create('2021-08-18', '1 month', Carbon::now()->toDateString());
foreach ($period as $date) {
$monthlyInvoices = Invoice::
whereRaw('YEAR(created_at) = ?', $date->format('Y'))
->whereRaw('MONTH(created_at) = ?', $date->format('m'));
$total_sales = $monthlyInvoices
->sum('net_price');
$offline_sales = $monthlyInvoices
->where('type', '=', 'offline')
->sum('net_price');
$online_sales = $monthlyInvoices
->where('type', '=', 'online')
->sum('net_price');
$invoiceStats = (new InvoiceMonthlyStatistics)->create([
'total_sales' => $total_sales,
'online_sales' => $online_sales,
'offline_sales' => $offline_sales,
'month' => $date->format('m'),
'year' => $date->format('y')
]);
}
$invoiceStats->save();
}
}
【问题讨论】:
-
when I swap the places of the $onlineInvoices and the $offlineInvoices...这些变量未显示在您的代码中,因此很难知道您在说什么。 “不工作”也不是对任何事物的有用描述。你的意思是它崩溃了?还是产生了一些意想不到的结果?要不然是啥?请澄清
标签: php sql laravel web backend