如何获取今年的所有文章?
Articles::all()->where('created_at',$current_year);
8dtrkrch1#
你可以这样做:
Articles::whereBetween('created_at', [ Carbon::now()->startOfYear(), Carbon::now()->endOfYear(), ]);
另一种方法是使用whereYear(),如@xhulio answer。但我建议你使用这段代码,因为它更易读:
whereYear()
Articles::whereYear('created_at', date('Y'))->get();
w8ntj3qf2#
尝试以下查询
Articles::whereYear('created_at', '=', Carbon::parse($date)->format('Y'))->get();
Laravel有辅助函数whereDate,whereDay,whereMonth和whereYear来更好地处理日期时间查询。
whereDate
whereDay
whereMonth
whereYear
ljo96ir53#
通过单线 Carbon 获得数据
Articles::whereYear('created_at', Carbon::now()->year)->get();
goqiplq24#
我发现这种方法收集所有的年份只是好的
@if(isset($years) && $years != '') @foreach($years as $yrs) @php($year[] = $yrs->created_at) @endforeach @if(isset($year)) @php( $collection = collect($year)->unique()->values()->all()) @foreach($collection as $created) @php($year_only[] = $created->format('Y')) @endforeach @php( $collections = collect($year_only)->unique()->values()->all()) @foreach($collections as $created_year) @if($created != '') <div class="lab-item"> <div class="lab-inner" style="border: #ee3725 2px solid;border-radius: 25px;background: bisque;"> <div class="lab-content"> <h4><a href="{{ url('years') }}/{{ $created_year }}"> {{ $created_year }} </a></h4> </div> </div> </div> @endif @endforeach @endif @endif
khbbv19g5#
/*In this code SurveyResult is model name and survey_results is table name and date('Y') mean current year. */ /* I hope its helpful for you :) */ SurveyResult::whereYear('survey_results.created_at', date('Y'))->get();
5条答案
按热度按时间8dtrkrch1#
你可以这样做:
另一种方法是使用
whereYear()
,如@xhulio answer。但我建议你使用这段代码,因为它更易读:w8ntj3qf2#
尝试以下查询
Laravel有辅助函数
whereDate
,whereDay
,whereMonth
和whereYear
来更好地处理日期时间查询。ljo96ir53#
通过单线 Carbon 获得数据
goqiplq24#
我发现这种方法收集所有的年份只是好的
khbbv19g5#