Php 在laravel中使用Elount查询关系日期

Php 在laravel中使用Elount查询关系日期,php,mysql,laravel,laravel-5,eloquent,Php,Mysql,Laravel,Laravel 5,Eloquent,我正在创建一个以laravel为API的日历应用程序 我希望能够搜索活动(活动模型)并过滤月份。所以我有一个搜索词和一个特定月份的数组来过滤。 此数组如下所示: array ( 0 => array ( 'first_date' => '2016-03-11', 'last_date' => '2016-03-31', ), 1 => array ( 'first_date' => '2016-04-01',

我正在创建一个以laravel为API的日历应用程序

我希望能够搜索活动(活动模型)并过滤月份。所以我有一个搜索词和一个特定月份的数组来过滤。 此数组如下所示:

array (
  0 => 
  array (
    'first_date' => '2016-03-11',
    'last_date' => '2016-03-31',
  ),
  1 => 
  array (
    'first_date' => '2016-04-01',
    'last_date' => '2016-04-30',
  ),
  2 => 
  array (
    'first_date' => '2016-05-01',
    'last_date' => '2016-05-31',
  )
  // etc...
)   
活动有多个日历项(s),表示活动的日期

现在我真的不确定在该数组的日期之间获取活动模型的最佳方法是什么

  • 我可以原始查询CalendarItems,并通过其雄辩的关系检索活动,然后我必须整理重复项
  • 我可以使用whereHas('CalendarItem',函数($q){$q->whereBetween(etc…)})
有人有好的建议吗

编辑2014年3月14日 相关迁移如下所示:
活动迁移:

 Schema::create('activities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title')->unique();
        $table->string('description')->nullable();
        $table->integer('activitytype_id');
        $table->integer('course_leader')->references('id')->on('users');
        $table->timestamps();
        $table->softDeletes();
    });
    Schema::create('calendar_items', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('activity_id')->references('id')->on('activities');
        $table->date('date');
        $table->time('starttime');
        $table->time('endtime');
    });
日历项迁移:

 Schema::create('activities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title')->unique();
        $table->string('description')->nullable();
        $table->integer('activitytype_id');
        $table->integer('course_leader')->references('id')->on('users');
        $table->timestamps();
        $table->softDeletes();
    });
    Schema::create('calendar_items', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('activity_id')->references('id')->on('activities');
        $table->date('date');
        $table->time('starttime');
        $table->time('endtime');
    });

此数组是不同活动的日期列表,还是说一个活动有多个开始和停止时间,并且您希望所有结果都在其中任何一个日期?每个日历项(属于一个活动)都有一个日期、开始时间和停止时间。我想获取在数组中给定的开始日期和停止日期之间具有calendaritem的活动。没有不属于calendaritem的活动。日历项是属于活动的日期。最后,我想在活动模型中添加一个带有日期数组参数的函数,在这里我可以向它提供上面给出的数组。不确定后面到底是什么,但第一点你可以使用
unique()
,这样你就不必处理DUP,或者对于第二点,您可以使用作用域,也可以使用
访问器和变异器。
但是,如果您可以包含一些有关如何工作的代码,您可以显示相关的迁移吗?日历项是否有“开始日期”和“结束日期”字段?就一个约会?