What about using the Build in Pagination from Laravel? https://laravel.com/docs/8.x/eloquent-resources#pagination
Pagination in Laravel
Hi, I need to do a pagination of some data I get from a DB. Since it's a lot of data, I wanted to do a pagination in one view. How can I do this? At the moment I have only created a pagination with 10 elements with:
$data=DataFromRasp::simplePaginate(10);
My controller is this:
class DeviceController extends Controller {
public function index()
{
$data=Device::all();
return view('backend.auth.user.device', compact("data"));
}
public function create()
{
}
public function store(Request $request)
{
}
public function show(Device $deviceID)
{
$device = Device::firstWhere('id', $deviceID);
return view('backend.auth.user.singleDevice', compact("device"));
}
public function edit(Device $device)
{
//
}
public function update(Request $request, Device $device)
{
//
}
public function destroy(Device $device)
{
//
}
public function visualizeData()
{
$data=DataFromRasp::simplePaginate(10);
return view('backend.auth.user.dictionary', compact("data"));
}
public function getData(Request $request)
{
$m_data = $request->get('m_data');
$r_data = $request->get('r_data');
DataFromRasp::create(['MAC' => $m_data, 'RSSI' => $r_data]);
if(($m_data == 'C4:A5:DF:24:05:7E' or $m_data == '70:1C:E7:E4:71:DA') and Device::where('MAC_ADDR', $request->m_data)->doesntExist()){
Device::create(['MAC_ADDR' => $m_data]);
}
}
public function scan()
{
$process = new Process(['python2','C:\Simone\Università\Smart IoT Devices\Lab_Raspy\Bluetooth\prova.py']);
$process->run();
if (!$process->isSuccessful()) { throw new ProcessFailedException($process); }
return redirect()->route('dict');
}
}
My route is:
Route::get('dict', [DeviceController::class, 'visualizeData'])->name('dict');
And the view is:
@extends('backend.layouts.app')
@section('content')
table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">MAC ADDRESS</th>
<th scope="col">RSSI</th>
</tr>
</thead>
<tbody>
@foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->MAC}}</td>
<td>{{$item->RSSI}}</td>
</tr>
@endforeach
</tbody>
<li class="page-item">
</li>
<li class="page-item"><a class="page-link" href="dict">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
For the moment, the last part of the view with the numbered list is just a preset of how I would like everything to look in the end.
Does anyone know how I could do this?
@Simone94 laravel comes with its own paginations, you can change your codes to this
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">MAC ADDRESS</th>
<th scope="col">RSSI</th>
</tr>
</thead>
<tbody>
@foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->MAC}}</td>
<td>{{$item->RSSI}}</td>
</tr>
@endforeach
</tbody>
{{ $data->links() }}
</table>
that's it if you're not using laravel 8
Please or to participate in this conversation.