Laravel 4中的分类集合
我有一个日历中的日期,并通过下面的内容访问它们,它们与一个类别表有一个类别关系,与一个CalEvent表有一个关系Laravel 4中的分类集合,laravel,eloquent,Laravel,Eloquent,我有一个日历中的日期,并通过下面的内容访问它们,它们与一个类别表有一个类别关系,与一个CalEvent表有一个关系 $cal_dates = CalDate::whereBetween('date', array($from, $to)) ->orderBy('date') ->orderBY('start') ->get(); 这将返回按范围内的开始日期排序的列表。到目前为止一切
$cal_dates = CalDate::whereBetween('date', array($from, $to))
->orderBy('date')
->orderBY('start')
->get();
这将返回按范围内的开始日期排序的列表。到目前为止一切正常,返回的行如下所示,我可以访问类别和CalEvent fine,即$cal_date->CalEvent->description
对于我的视图,我需要的是一个如下所示的对象,它与Category和Cal_Dates表的关系保持不变:
cal_dates
'12th Feb 2014'
'Array of dates related to category 1 for 12th Feb'
'Array of all other dates not related to category 1 for 12th Feb'
'13th Feb 2014'
'Array of dates related to category 1 for 13th Feb'
'Array of all other dates not related to category 1 for 13th Feb'
...
非常感谢您的任何建议……试试以下方法:
$cal_dates = $cal_dates->sortBy(function($cal_date)
{
return $cal_date->date;
});
或修改您的查询:
然后,在您的视图中,您可以访问关系,如下所示:
@foreach($cal_dates as $cal_date)
<li> {{ $cal_date->date }} </li>
<!-- category data -->
<li> {{ $cal_date->category->something }}
@endforeach
试试这个:
$cal_dates = $cal_dates->sortBy(function($cal_date)
{
return $cal_date->date;
});
或修改您的查询:
然后,在您的视图中,您可以访问关系,如下所示:
@foreach($cal_dates as $cal_date)
<li> {{ $cal_date->date }} </li>
<!-- category data -->
<li> {{ $cal_date->category->something }}
@endforeach
嗨,什么是“2014年2月12日”?创建日期、更新日期?它是CalDate-中的一列,即$cal\u date->date=2014-04-06嗨,“2014年2月12日”是什么?创建日期、更新日期?它是CalDate-中的一列,即$cal\u date->date=2014-04-06