Simone94's avatar

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">&raquo;</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?

0 likes
8 replies
Simone94's avatar

@tomasnorre I would prefer to use the Paginator because by now I have set up the project in this way

tomasnorre's avatar

@Simone94 Ok. I don't see what that has to do with the project set up. But if it isn't solving your problem. Sorry I couldn't help.

Simone94's avatar

@tomasnorre Sorry maybe I didn't explain myself well I mean that using the method that you have proposed I would have to change too many things compared to what I have already set up. I am a Laravel neophyte so I could be wrong.

vainway 's avatar
vainway
Best Answer
Level 4

@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.