Method Illuminate\Database\Eloquent\Collection::appends does not exist

Method Illuminate\Database\Eloquent\Collection::appends does not exist. It found this error in my controller file because I did not used paginate() method on collection. Add paginate method on collection object before calling the appends method in controller to avoid error in laravel pagination

Answers 1
  • Pass query string with pagination in laravel controller

    //routes\web.php
    use App\Http\Controllers\HomeController;
    Route::get('/pagination-with-query-string', [HomeController::class, 'getRecords']);
    
    //app\Http\Controllers\HomeController.php
    use App\Models\User;
    public function getRecords(){
        $users = User::select('*')->paginate(10);
        $users = $users->appends(['keyword'=>'value']);
        return view('search')->with(['users'=>$users]);
    }
    
    //resources\views\search.blade.php
    <table>
        @foreach ($users as $user)
        <tr>
            <td> {{ $user->name }}  </td>
        </tr>
        @endforeach
    </table>
    {{ $users->links() }}
    
    0

    You can add query string to URL in laravel using appends() method in controller file. 

  • Back to code snippet queries related laravel