我将在控制器索引函数中放入paginate方法来查看我的数据。我已经用分页配置了所有的东西,但我无法猜测我应该把下面的分页放在索引函数的哪里
paginate(10);
我的索引函数,
public function index()
{
$vehicles = Vehicle::with('uploads')->get()->sort(function ($a, $b) {
if ($a->adtype !== $b->adtype) {
return $b->adtype - $a->adtype;
}
return $a->adtype
? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
: ($a->created_at->gt($b->created_at) ? -1 : 1);
});
return view('vehicles.index')->withVehicles($vehicles);
}
我应该把它们放在哪里?
视图
@扩展("layouts.app"(
@section('content')
@forelse( $vehicles as $vehicule )
@if( $vehicule->uploads->count() > 0 )
<a href="{{ route('vehicles.show', $vehicule->id) }}">
@php
$upload = $vehicule->uploads->sortByDesc('id')->first();
@endphp
<div style="border-style: solid; color: {{ $vehicule->adtype === 1 ? 'black' : 'blue' }} ">
<img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
{{ CarbonCarbon::parse($vehicule->created_at)->diffForHumans()}}
{{$vehicule->provincename}}
{{$vehicule->milage}}
</div>
<br>
<hr>
@endif
@empty
<td>No Advertisment to display.</td>
@endforelse
</div>
</div>
</div>
{{$vehicule->links()}} //paginate showing links
</div>
@endsection
只需将get()
方法替换为paginate()
,如
$vehicles = Vehicle::with('uploads')->paginate()->sort(function($a, $b) { return })
并正常地编写代码的其余部分