Laravel 6 查询相同 table 但 where 子句不同
Laravel 6 query same table with different where clause
我有campaign_report
table这样
Schema::create('campaign_report', function (Blueprint $table) {
$table->bigIncrements('id');
$table->date('report_date');
$table->bigInteger('user_id');
$table->bigInteger('campaignId');
$table->double('cost',12,2);
$table->timestamps();
});
我正在尝试获取昨天的所有活动报告。
$campaignReports = CampaignReport::where(['report_date' => "$yesterday", 'user_id' => Auth::user()->id])->orderBy('report_date', 'desc')->paginate(25);
以上查询返回所有 report_date 等于 $yesterday.
的活动
除了这个查询之外,我还想获取每个活动的 cost
值,其中 report_date
是前天匹配 campaignId
列。我想显示昨天的广告系列和前天的广告系列之间 cost
的区别。
喜欢
foreach($campaignReports as $campaignReport)
{
$difference = $campaignReport->cost - $campaignReport->dayBeforeYesterdayCost;
}
任何人都可以帮助我使用查询生成器或 Eloquent 构建此查询吗?
您可以在您的 CampaignReport
模型中编写一个关系,计算昨天和前一天之间的差异
public function dayBeforeYesterdayCost() {
$dayBeforeYesterday = Carbon::parse($this->report_date)->subDays(1); //return date of day before yesterday
$dayBeforeYesterdayCost = CampaignReport::where('campaignId', $this->campaignId)
->where('report_date', $dayBeforeYesterday)
->first();
return $dayBeforeYesterdayCost ? $dayBeforeYesterdayCost->cost : 0;
}
public function lastDayDifference() {
$difference = $this->cost - $this->dayBeforeYesterdayCost;
return $difference;
}
现在您可以 return $campaignReports
与 lastDayDifference
关系如下:
$campaignReports = CampaignReport::where(['report_date' => "$yesterday", 'user_id' => Auth::user()->id])
->with('lastDayDifference')
->orderBy('report_date', 'desc')
->paginate(25);
最后你的每一个都附上了一个对象$campaignReport
Group by date(report_date) 这只会给你日期部分,按相同的降序排列,然后只取昨天和前一天的值。类似于:
CampaignReport::select(DB::raw('date(report_date) report_date', DB::raw('count(*) as count'))
->where('report_date','>=',$twoDaysAgo)
->where('user_id', Auth::user()->id])
->groupBy('report_date')
->orderBy('report_date', 'desc');
我有campaign_report
table这样
Schema::create('campaign_report', function (Blueprint $table) {
$table->bigIncrements('id');
$table->date('report_date');
$table->bigInteger('user_id');
$table->bigInteger('campaignId');
$table->double('cost',12,2);
$table->timestamps();
});
我正在尝试获取昨天的所有活动报告。
$campaignReports = CampaignReport::where(['report_date' => "$yesterday", 'user_id' => Auth::user()->id])->orderBy('report_date', 'desc')->paginate(25);
以上查询返回所有 report_date 等于 $yesterday.
的活动除了这个查询之外,我还想获取每个活动的 cost
值,其中 report_date
是前天匹配 campaignId
列。我想显示昨天的广告系列和前天的广告系列之间 cost
的区别。
喜欢
foreach($campaignReports as $campaignReport)
{
$difference = $campaignReport->cost - $campaignReport->dayBeforeYesterdayCost;
}
任何人都可以帮助我使用查询生成器或 Eloquent 构建此查询吗?
您可以在您的 CampaignReport
模型中编写一个关系,计算昨天和前一天之间的差异
public function dayBeforeYesterdayCost() {
$dayBeforeYesterday = Carbon::parse($this->report_date)->subDays(1); //return date of day before yesterday
$dayBeforeYesterdayCost = CampaignReport::where('campaignId', $this->campaignId)
->where('report_date', $dayBeforeYesterday)
->first();
return $dayBeforeYesterdayCost ? $dayBeforeYesterdayCost->cost : 0;
}
public function lastDayDifference() {
$difference = $this->cost - $this->dayBeforeYesterdayCost;
return $difference;
}
现在您可以 return $campaignReports
与 lastDayDifference
关系如下:
$campaignReports = CampaignReport::where(['report_date' => "$yesterday", 'user_id' => Auth::user()->id])
->with('lastDayDifference')
->orderBy('report_date', 'desc')
->paginate(25);
最后你的每一个都附上了一个对象$campaignReport
Group by date(report_date) 这只会给你日期部分,按相同的降序排列,然后只取昨天和前一天的值。类似于:
CampaignReport::select(DB::raw('date(report_date) report_date', DB::raw('count(*) as count'))
->where('report_date','>=',$twoDaysAgo)
->where('user_id', Auth::user()->id])
->groupBy('report_date')
->orderBy('report_date', 'desc');