laravel通过1个查询按日期分组模型

b1uwtaje  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我想知道是否有可能用一个查询按日期分组约会,所以我的回答是这样的(在json中):

{  
   "appointmentsByDay":[  
      "01-07-2018":[  
         {  
            "appointment":"test"
         },
         {  
            "appointment":"test"
         }
      ],
      "02-07-2018":[  
         {  
            "appointment":"test"
         },
         {  
            "appointment":"test"
         }
      ],
      ..................
      "31-07-2018":[  
         {  
            "appointment":"test"
         },
         {  
            "appointment":"test"
         }
      ]
   ]
}

我知道这是可能的循环通过所有天在一个月内,但然后我不得不调用一个类似的查询多达31次为一个月。
目前,我的所有预约都是在一个月内按以下方式进行的,但这些预约并不是按天分组的,如果预约范围较大,则会被错误地安排:

$startOfMonth = Carbon::parse($date)->startOfMonth();
$endOfMonth = Carbon::parse($date)->endOfMonth();

$appointments = Appointment::
    where('user_id', '=', $userId)->
    where(function ($query) use($endOfMonth, $startOfMonth) {
        $query->where(function ($query) use($endOfMonth, $startOfMonth) {
            $query->where('from_date', '<', $startOfMonth->format('Ymd'))
                  ->where('to_date', '>', $endOfMonth->format('Ymd'));
        })
        ->orWhere(function ($query) use($endOfMonth, $startOfMonth) {
            $query->where('from_date', '>=', $startOfMonth->format('Ymd'))
                  ->where('to_date', '<=', $endOfMonth->format('Ymd'));
        })
        ->orWhere(function ($query) use($endOfMonth, $startOfMonth) {
            $query->where('from_date', '<', $startOfMonth->format('Ymd'))
                  ->where('to_date', '>=', $startOfMonth->format('Ymd'));
        })
         ->orWhere(function ($query) use($endOfMonth, $startOfMonth) {
             $query->where('from_date', '<=', $endOfMonth->format('Ymd'))
                  ->where('to_date', '>', $endOfMonth->format('Ymd'));
        });
    })
    ->with(user')
    ->get();

我的模型是这样的:

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;

class Appointment extends Eloquent
{
    protected $table = 'appointments';
    public $timestamps = false;

    protected $fillable = [
        'from_date',
        'to_date',
        'title'
    ];
}

我的约会表如下所示:(我使用的是mysql)

+-----------+----------------+---------------------+-------------------+
| id (int)  | title (varchar)| from_date (varchar) | to_date (varchar) |
+-----------+----------------+---------------------+-------------------+
| 1         | appointment1   | 20180725            | 20180725          |
+-----------+----------------+---------------------+-------------------+
| 2         | appointment2   | 20180726            | 20180726          |
+-----------+----------------+---------------------+-------------------+
| 3         | appointment3   | 20180723            | 20180812          |
+-----------+----------------+---------------------+-------------------+
| 4         | appointment4   | 20180726            | 20180726          |
+-----------+----------------+---------------------+-------------------+
| 5         | appointment5   | 20180612            | 20181123          |
+-----------+----------------+---------------------+-------------------+

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题