我将使用分页方法在控制器索引函数中查看数据。我已经为所有内容配置了paginate,但是我无法猜测应该在索引函数中放置以下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);
}
我应该放在哪里?
视图
@extends('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>
{{ Carbon\Carbon::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
答案 0 :(得分:0)
只需将get()
方法替换为paginate()
$vehicles = Vehicle::with('uploads')->paginate()->sort(function($a, $b) { return })
并正常编写其余代码