【问题标题】:Is there a problem with the $monthlyInvoices variable?$monthlyInvoices 变量有问题吗?
【发布时间】: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


【解决方案1】:

这是因为$monthlyInvoices 是一个查询构建器。

当您添加where 语句时,即使在您调用sum 后它仍然存在:

$offline_sales = $monthlyInvoices
                ->where('type', '=', 'offline') //here
                ->sum('net_price');

这就是问题所在,因为当你这样做时:

$online_sales = $monthlyInvoices
                ->where('type', '=', 'online')
                ->sum('net_price');

-&gt;where('type', '=', 'offline') 还在这里,因为你之前添加了它。

从某种意义上说,你的第二个查询和做的一样:

$online_sales = $monthlyInvoices
                ->where('type', '=', 'offline')
                ->where('type', '=', 'online')
                ->sum('net_price');

现在您可能在这里看到了问题。解决此问题的一种快速方法是在每次查询之前克隆查询生成器。

$offline_sales = (clone $monthlyInvoices)
                ->where('type', '=', 'offline')
                ->sum('net_price');

        
$online_sales = (clone $monthlyInvoices)
                ->where('type', '=', 'online')
                ->sum('net_price');

通过克隆$monthlyInvoices,您基本上是在说“不要使用相同的查询生成器,而是使用相同的新查询生成器”。此临时查询构建器将仅用于此特定查询,where('type', '=', 'offline')(或您所做的任何事情)不会“持久化”到原始 $monthlyInvoices 查询构建器中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2018-07-09
    • 2016-07-03
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多