LaraBABA's avatar

Pagination keep looping with the same results - Laravel 5.5

Hello,

I wonder if anyone had the same problem please.

I only have 15 results in my db and the pagination numbers are showing from 1 to 137 pages. When I click on a number, it shows the same results again and again.

here is my code

    public function index()
    {

        $users = DB::table('users')
        ->join('roles', 'roles.id', '=', 'users.role_id')
        ->join('towns', 'towns.id', '=', 'towns.id')
        ->select('users.*', 'roles.name AS role_name', 'towns.town')
        ->paginate(15);

        return view('admin.users-view', ['users' => $users]);
   
    }

<tbody>
                      @foreach ($users as $user)
                      <tr>
                      <td class="col-md-1">{{ $user->id }}</td>
                      <td class="col-md-1">{{ $user->username }}</td>
                      <td class="col-md-1">{{ $user->name }}</td>
                      <td class="col-md-1">{{ $user->surname }}</td>
                      <td class="col-md-2">{{ $user->email }}</td>
                      <td class="col-md-2">{{ $user->town }}</td>
                      @if($user->isactivated == 1)
                      <td class="col-md-1"><button class="btn btn-success" type="submit">Activated</button></td>
                      @else
                      <td class="col-md-1"><button class="btn btn-danger" type="submit">Not Activated</button></td>
                      @endif
                      <td class="col-md-1">{{ $user->role_name }}</td>
                      <td class="col-md-1"><a href="{{ action('Admin\UserController@edit', $user->id) }}" class="btn btn-warning" type="submit">Edit</a></td>
                      <td class="col-md-1">
                      <form action="{{ action('Admin\UserController@destroy', $user->id) }}" method="post">
                      {{csrf_field()}}
                      <input name="_method" type="hidden" value="DELETE">
                      <button class="btn btn-danger" type="submit" onclick="var result = confirm('Do you really want to delete this user?')">Delete</button>
                      </form>
                      </td>
                      </tr>
                      @endforeach
                      </tbody>
                      </table>
                           </div>
                      <div class="container">
                      <nav aria-label="Page navigation">
                      <ul class="pagination">
                      <?php echo $users->appends(['sort' => 'users'])->render(); ?>
                      </ul>
                      </nav>
                      </div>

Thank you for your help.

0 likes
3 replies
Snapey's avatar

what if you use the standard method of creating the links? {{ $users->links() }}

1 like
mushood's avatar

Try dd($users). Maybe the join is returning more data.

In that case, try use distinct()

      $users = DB::table('users')
        ->join('roles', 'roles.id', '=', 'users.role_id')
        ->join('towns', 'towns.id', '=', 'towns.id')
        ->select('users.*', 'roles.name AS role_name', 'towns.town')
        ->distinct()
        ->paginate(15);
LaraBABA's avatar

Thank you!

Well it turns out that my query was wrong, here is the new one: From

        $users = DB::table('users')
        ->join('roles', 'roles.id', '=', 'users.role_id')
        ->join('towns', 'towns.id', '=', 'towns.id')
        ->select('users.*', 'roles.name AS role_name', 'towns.town')
        ->paginate(15);

To

        $users = DB::table('users')
        ->join('towns', 'users.town_id', '=', 'towns.id') <--
        ->join('roles', 'users.role_id', '=', 'roles.id')<--
        ->select('users.*', 'roles.name AS role_name', 'towns.town')
        ->distinct()
        ->paginate(5);

Now I do not know if this can be of any use to you but I have found a free a wonderful query builder which wrote the above for me. You simply drag your tables on the screen, set the joins and click the fields you wish to output in the query. This way no more mistakes.

Look online for Valentino Studio(free edition).

I know you guys are way above my level, but you never know....it might come handy to you on complex queries.

It literally took me 30 seconds to setup the software and have it to write the query for me.

Please or to participate in this conversation.