Seeker1337's avatar

Laravel 6+ Pagination on page and CRUD problems

Hello there. Basically I have a project related to vehicles and the structure is like this:

I have a CRUD system in a Management page where I add vehicles using a form and they get displayed in another page in my website. However, I've came to an issue and I'd need your help:

  1. In my CRUD system I need to make a pagination for up to 15 vehicles per a page ( lets say I have 90 vehicles, so I would need 6 pages ).
  2. In the page where the vehicles get displayed in my website I need to have a pagination for up to 30 vehicles per a page.
  3. In my VehController.php when I try the following code " $vehmodel = VehModel::all() ->paginate(15) " I get the following error " Method Illuminate\Database\Eloquent\Collection::paginate does not exist. ". I'm assuming I'm not adding the paginate method at the right place.
  4. Can I make two different paginations using one controller? In my case 'VehController.php'

I've been reading about Laravel's pagination but got confused with using the paginate method on the query builder or an Eloquent query. Here is my code:

VehController.php

<?php

namespace App\Http\Controllers ;

use App\VehModel ;
use Illuminate\Http\Request ;

class VehController extends Controller
{
    
    public function display() {
        
        return view('vehicles')  ; // the page in my website where vehicles get displayed
        
    }
    
    public function index()
    {
        
        $vehmodel = VehModel::all() ; // tried adding pagination(15) here but I get an error
        
        return view('management', compact('vehmodel')) ; // management page
        
    }
    
    public function store(Request $request)
    {
        
        $vehmodel = new VehModel ;
        $vehmodel->vehmod = $request->vehmod ;
        $vehmodel->vehmark = $request->vehmark ;
        $vehmodel->vehyear = $request->vehyear ;
        $vehmodel->vehengine = $request->vehengine ;
        $vehmodel->vehtrans = $request->vehtrans ;
        $vehmodel->vehimage = $request->vehimage ;
        
        $file = $vehmodel->vehimage = $request->vehimage ;
        $filename = $file->getClientOriginalName() ;
        $destinationPath = 'storage/veh-images' ; 
        $vehmodel->vehimage = $filename; 
        $uploadSuccess = $file->move($destinationPath, $filename);
        
        $vehmodel->save() ;

        return redirect('management')->with('success', 'The record has been created succesfully!') ;
    }

    public function show($id)
    {
        $vehmodel = VehModel::findOrFail($id) ;
        
        return view('crud.show', compact('vehmodel')) ;
    }

    public function edit($id)
    {
        
        $vehmodel = VehModel::findOrFail($id) ;
        
        return view('crud.edit', compact('vehmodel')) ;
        
    }

    public function update(Request $request, $id)
    {
        
        $vehmodel = VehModel::findOrFail($id) ;
        
        $vehmodel->update($request->all()) ;
        
        return redirect('management')->with('success', 'The record has been redacted succesfully!') ;
        
    }

    public function destroy($id)
    {
        
        $vehmodel = VehModel::whereId($id)->delete() ;
        
        return redirect('management')->with('warning', 'The record has been deleted succesfully!') ;
        
    }
}


index.blade.php

<?php use App\VehModel ?>

<div id="crud-onclick-display2"> 
    <table id="customers">
    
<tr>
    <th> № </th>
    <th> Model </th>
    <th> Mark </th>
    <th> Year </th>
    <th> Engine </th>
    <th> Transmission </th>
    <th> Picture </th>
    <th> Actions </th>
</tr>
    
<?php   
    $vehmodel = VehModel::all() ;
    foreach ($vehmodel as $VehModel) {
?>    
    
<tr>
    
    <td> {{ $VehModel->id }} </td>
    <td> {{ $VehModel->vehmod }} </td>
    <td> {{ $VehModel->vehmark }} </td>
    <td> {{ $VehModel->vehyear }} </td>
    <td> {{ $VehModel->vehengine }} </td>
    <td> {{ $VehModel->vehtrans }} </td>
    <td> 
        <div class="image-container">
        <img src="storage/veh-images/{{ $VehModel->vehimage }}" height="50%" width="50%" alt="">
        </div> 
    </td> 
    <td> <a href="{{ route('management.show', $VehModel->id) }}"> <button type="button" class="btn btn-primary"> Edit </button> </a> 
         <form action="{{ route('management.destroy', $VehModel->id) }}" method="post">
             @CSRF
             @method('DELETE')    

             <input type="submit" name="submit" value="Delete" style="padding:6px 6px; width:63%; background-color:red; color:white">
         </form> 
    </td> 
    
</tr>
 
<?php  }  ?>   
    
    
</table>
</div>  


0 likes
9 replies
a4ashraf's avatar

@seeker1337

you are using the wrong statement for pagination

In my VehController.php when I try the following code " $vehmodel = VehModel::all() ->paginate(15) " I get the following error " Method Illuminate\Database\Eloquent\Collection::paginate does not exist. ". I'm assuming I'm not adding the paginate method at the right place.


// Use this following 

$vehmodel = VehModel::paginate(15);

Seeker1337's avatar

I've already tried that but basically nothing happens. When I also try to add $VehModel->links() or renders() in my index.blade.php I get the following error "Call to undefined method App\VehModel::render()"

a4ashraf's avatar

@seeker1337

you should debug your code like this


 public function index()
    {
        
        $vehmodel = VehModel:: paginate(15) ; 

    dd($vehmodel) // see what its print 
        
        return view('management', compact('vehmodel')) ; // management page
        
    }

also share your model and DB schema as well

shushkin's avatar

@seeker1337 Hi

replace your view code with next one:

<div id="crud-onclick-display2"> 
    <table id="customers">
<tr>
    <th> № </th>
    <th> Model </th>
    <th> Mark </th>
    <th> Year </th>
    <th> Engine </th>
    <th> Transmission </th>
    <th> Picture </th>
    <th> Actions </th>
</tr>
    
@foreach($vehmodel as $VehModel)      
    <tr>
    
    <td> {{ $VehModel->id }} </td>
    <td> {{ $VehModel->vehmod }} </td>
    <td> {{ $VehModel->vehmark }} </td>
    <td> {{ $VehModel->vehyear }} </td>
    <td> {{ $VehModel->vehengine }} </td>
    <td> {{ $VehModel->vehtrans }} </td>
    <td> 
        <div class="image-container">
        <img src="storage/veh-images/{{ $VehModel->vehimage }}" height="50%" width="50%" alt="">
        </div> 
    </td> 
    <td> <a href="{{ route('management.show', $VehModel->id) }}"> <button type="button" class="btn btn-primary"> Edit </button> </a> 
         <form action="{{ route('management.destroy', $VehModel->id) }}" method="post">
             @CSRF
             @method('DELETE')    

             <input type="submit" name="submit" value="Delete" style="padding:6px 6px; width:63%; background-color:red; color:white">
         </form> 
    </td> 
</tr>
@endforeach
</table>
</div>

You are just overriding $vehmodel in view file or just here

<?php   
    $vehmodel = VehModel::all() ;
    foreach ($vehmodel as $VehModel) {
?>  

try to change to

<?php
    foreach ($vehmodel as $VehModel) {
?>  

If i understood your problem correct

shushkin's avatar
shushkin
Best Answer
Level 5

in controller

public function index()
    {
        
        $vehmodel = VehModel::paginate(15);
        
        return view('management', compact('vehmodel')) ; // management page
        
    }

in view

<?php   
    $vehmodel = VehModel::paginate(30) ;
    foreach ($vehmodel as $VehModel) {
?>    
Seeker1337's avatar

Oh right what a mistake, thank you mate. It's working now but I don't get the output of [Next], [Previous] and the pages numbers themselves. Does this come with the in-built paginate or I must add something else as well? I also added $vehmodel->links() but nothing happens.

Robstar's avatar

Actually, if you just want previous and next links, with the actual page numbers, use simplePaginate() instead of paginate().

Seeker1337's avatar

I forgot to echo the link function and that's why I wasnt getting any output. Thank you all for the help. Problem solved.

Please or to participate in this conversation.