如何在 laravel 查询生成器或 eloquent 中用两个表按 created_at 列计算行数?
How to count the rows by created_at columns with two tables in laravel query builder or eloquent?
我有 call_logs 求职者 tables.Both table 有 created_at column.And call_logs jobseeker_id table.
在Jobseeker.php
public function calllogs()
{
return $this->hasMany('App\Calllog','jobseeker_id');
}
在Calllog.php
public function jobseeker()
{
return $this->belongsTo('App\Jobseeker','jobseeker_id');
}
我想展示一个 table 喜欢
created_at call_logs jobseekers
----------------------------------- ---------------------------------
2017-08-05 1 3
2017-08-04 2 2
2017-08-03 3 1
2017-08-02 2 4
2017-08-01 5 8
所以,我可以跟踪记录,每天有多少 call_logs 和多少新求职者。
这是我在一个 table 中尝试的方法,它的工作 well.But 我想一次查询两个 tables,
$count = DB::table('calllogs')
->select('created_at', DB::raw('count(*) as calllogs'))
->get();
它可以给我看,喜欢
created_at calllogs
----------------------------------- ----------
2017-08-03 2
2017-08-04 3
2017-08-05 8
2017-08-06 6
那么请指导我一种方法吗?我应该使用联合吗?提前致谢。
有两种方法可以解决这个问题。两者都需要一些编码。
首先您进行 2 个不同的查询,例如也进行此查询。
$count2 = DB::table('jobseekers')
->select(DB::raw('DATE(created_at) as created_at'), DB::raw('count(*) as jobseekers'))
->groupBy(created_at'))
->get();
现在您按天对齐计数并将其显示在 table 中。
另一种选择是每天查询并计算当天的通话记录和求职者条目。
$start_date = Carbon::now()->subWeek();
$end_date = Carbon::now()->now();
$dates = [];
for ( $date = $start_date; $date->lte( $end_date ); $date->addDay() ) {
$dates[] = $date);
}
$counts = [];
foreach ( $dates as $date ) {
$dateLater = $date->copy()->addDay();
$callLogs = Calllog::whereBetween( [$date, $dateLater] )->count();
$jobSeekers = Jobseeker::whereBetween( [$date, $dateLater] )->count();
$counts[ $date->format( 'Y-m-d' ) ] = [ $callLogs, $jobSeekers ];
}
此处 $counts 包含范围内每一天的求职者和通话记录数。
我有 call_logs 求职者 tables.Both table 有 created_at column.And call_logs jobseeker_id table.
在Jobseeker.php
public function calllogs()
{
return $this->hasMany('App\Calllog','jobseeker_id');
}
在Calllog.php
public function jobseeker()
{
return $this->belongsTo('App\Jobseeker','jobseeker_id');
}
我想展示一个 table 喜欢
created_at call_logs jobseekers
----------------------------------- ---------------------------------
2017-08-05 1 3
2017-08-04 2 2
2017-08-03 3 1
2017-08-02 2 4
2017-08-01 5 8
所以,我可以跟踪记录,每天有多少 call_logs 和多少新求职者。 这是我在一个 table 中尝试的方法,它的工作 well.But 我想一次查询两个 tables,
$count = DB::table('calllogs')
->select('created_at', DB::raw('count(*) as calllogs'))
->get();
它可以给我看,喜欢
created_at calllogs
----------------------------------- ----------
2017-08-03 2
2017-08-04 3
2017-08-05 8
2017-08-06 6
有两种方法可以解决这个问题。两者都需要一些编码。
首先您进行 2 个不同的查询,例如也进行此查询。
$count2 = DB::table('jobseekers')
->select(DB::raw('DATE(created_at) as created_at'), DB::raw('count(*) as jobseekers'))
->groupBy(created_at'))
->get();
现在您按天对齐计数并将其显示在 table 中。
另一种选择是每天查询并计算当天的通话记录和求职者条目。
$start_date = Carbon::now()->subWeek();
$end_date = Carbon::now()->now();
$dates = [];
for ( $date = $start_date; $date->lte( $end_date ); $date->addDay() ) {
$dates[] = $date);
}
$counts = [];
foreach ( $dates as $date ) {
$dateLater = $date->copy()->addDay();
$callLogs = Calllog::whereBetween( [$date, $dateLater] )->count();
$jobSeekers = Jobseeker::whereBetween( [$date, $dateLater] )->count();
$counts[ $date->format( 'Y-m-d' ) ] = [ $callLogs, $jobSeekers ];
}
此处 $counts 包含范围内每一天的求职者和通话记录数。