如何获取当年的所有记录?

时间:2016-12-26 11:37:23

标签: laravel laravel-5 laravel-5.2

如何获取当年的所有文章?

Articles::all()->where('created_at',$current_year);

3 个答案:

答案 0 :(得分:9)

你可以这样做:

^0+

另一种方法是在@xhulio回答中使用var i = 0; $('#dragbar').mousedown(function(e){ $('#mousestatus').html("mousedown" + i++); $(document).mousemove(function(e){ $('#position').html(e.pageY +', '+ e.pageX); $('#sidebar').css("height",e.pageY-2); $('#main').css("top",e.pageY+12); }) console.log("leaving mouseDown"); }); $(document).mouseup(function(e){ $('#clickevent').html('in another mouseUp event' + i++); $(document).unbind('mousemove'); }); 。但我建议您使用此代码,因为它更具可读性:

Articles::whereBetween('created_at', [
    Carbon::now()->startOfYear(),
    Carbon::now()->endOfYear(),
]);

答案 1 :(得分:1)

尝试以下查询

Articles::whereYear('created_at', '=', Carbon::parse($date)->format('Y'))->get();

Laravel具有帮助函数whereDatewhereDaywhereMonthwhereYear,可以更好地处理日期时间查询。

答案 2 :(得分:0)

我发现这种仅收集所有年份的方法很好

@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