Seeker1337
6 months ago
1272
9
Laravel

Laravel 6+ Pagination on page and CRUD problems

Posted 6 months ago by Seeker1337

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>  


Please sign in or create an account to participate in this conversation.