我遇到的一个问题可能是很多人已经在这里讨论了,但是我似乎无法弄清楚。
我正在尝试在同一个变量中使用Carbon和Paginate,但始终得到标题中显示的错误。
我尝试使用的代码是:
控制器:
public function index()
{
$announcements = Announcement::withCount('replies')->orderBy('created_at', 'desc')->paginate(5);
$birthdays = User::whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(birthday) AND DAYOFYEAR(curdate()) + 365 >= dayofyear(birthday)')
->orderByRaw('DAYOFYEAR(birthday)')
->get();
$date = Carbon::create($announcements->created_at)->locale('nl');
return view('home', compact('announcements', 'birthdays', 'date'));
}
查看:
@foreach($announcements as $announcement)
<div class="announcement">
<div class="row">
<div class="col-lg-11">
<h2 style="font-size:1.5rem;" class="text-capitalize">{{$announcement->post_title}}</h2>
@if($announcement->post_image == null)
@else
<img src="{{$announcement->post_image}}" style="width:100%;">
@endif
<p style="font-size: 0.8rem;">{{$birthday->isoFormat('LL')}} | Geplaatst door <span>{{$announcement->username}}</span> | <span style="color:#007ac3">{{$announcement->replies_count}} reacties</span></p>
<p style="margin-top: -10px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">{!! Str::words($announcement->post_content, 20) !!}</p>
</div>
<div class="col-lg-1">
@if(Auth::user()->admin == 1)
<a href="/mededeling/destroy/{{$announcement->id}}"><i class="fal fa-dumpster" style="text-align: center;position: relative;font-size: 20px;"></i></a>
@endif
</div>
</div>
<p><a href="/mededeling/{{$announcement->slug}}">Meer lezen <i class="fas fa-angle-double-right"></i></a></p>
<hr>
</div>
@endforeach
我希望它可以将日期转换为荷兰语,但是现在我得到的只是错误。
答案 0 :(得分:0)
-> paginate()方法返回LengthAwarePaginator对象,而不是单个模型。您需要将date函数映射到Collection对象中的每个元素上。