Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从@foreach Laravel 5.8获取唯一值_Laravel - Fatal编程技术网

如何从@foreach Laravel 5.8获取唯一值

如何从@foreach Laravel 5.8获取唯一值,laravel,Laravel,我试图在下拉列表中获取唯一的值。下拉列表的代码为: @foreach ($allCourses as $courses) <option>{{ \Carbon\Carbon::parse( $courses -> start_date )->format('F') }}</option> @endforeach 这给了我所有的价值观。例如,如果在同一个月有多个条目,它会给出如下结果 八月 八月 八月 九月 九月 但是如果同一个月有多个条目,我希望它们

我试图在下拉列表中获取唯一的值。下拉列表的代码为:

@foreach ($allCourses as $courses)
    <option>{{ \Carbon\Carbon::parse( $courses -> start_date )->format('F') }}</option>
@endforeach
这给了我所有的价值观。例如,如果在同一个月有多个条目,它会给出如下结果

八月

八月

八月

九月

九月

但是如果同一个月有多个条目,我希望它们在下拉列表中显示一次。因此,结果如下:

八月

九月

您可以在集合上尝试一种独特的()方法

$allCourses = Courses::latest() -> get()->unique('start_date');

您可以使用此查询筛选月份

$months= Courses::select(DB::raw('MONTHNAME(start_date) month_name'))
        ->groupBy('month_name')
        ->orderBy('month_name', 'desc')
        ->get();
并在blade中获取月份名称

@foreach ($months as $month)
    <option>{{ $month->month_name }}</option>
@endforeach
@foreach($months作为$month)
{{$month->month_name}
@endforeach

您可以检索唯一的
月份
,但您会将哪些
课程
字段作为
选项
标记的
?。因为我认为你想给
选择选项赋予
属性,否则你的下拉列表不会选择任何内容。首先感谢你的回复,我知道这个代码,但需要的只是唯一的月份,不全是日期谢谢你的工作,非常感谢你的帮助
@foreach ($months as $month)
    <option>{{ $month->month_name }}</option>
@endforeach