Method Illuminate\Support\Collection::offset does not exist

11,957

Illuminate\Support\Collection::offset does not exist. it is because your $users is instance of Illuminate\Support\Collection. For check it use dd(get_class($users)); after $totalItems = $users->count(); It must be print Illuminate\Support\Collection. Collection instance has count method, which return Collection items count. For your $users->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get() code work right you must be change $users to $query(maybe $users) where $query(maybe $users) isntance of \Illuminate\Database\Query\Builder.
For this case use

$query->->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();

Your corrected code is

public function membrevis()
{
    $filter = isset($_GET['filter']) ? $_GET['filter'] : null;
    $query = DB::table('users')
        ->join('user_role', 'users.id', '=', 'user_role.user_id')
        ->join('roles', 'user_role.role_id', '=', 'roles.id')
        ->where('users.valid','=',0)
        ->select('users.*','roles.description');
    if ($filter != null) {
        $query->where('users.name','like','%'.$filter.'%')
            ->orWhere('roles.description','like','%'.$filter.'%');
    }

    $itemsPerPage = 8 ;
    $currentPage  = isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ? $_GET['page'] : 1;
    $urlPattern   = '/profilecontroller/membrevis?page=(:num)';
    $totalItems   = $query->count();
    $donner   = $query->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();
    $paginator = new  Paginator( $totalItems, $itemsPerPage, $currentPage, $urlPattern );

    return view('membre2',['users'=> $donner,'paginator'=> $paginator]);
}

I delete $users = $query->get() method which is one more request in db. And for get total user count you used $totalItems = $query->count(); which is more optimized query

Share:
11,957
H.tay
Author by

H.tay

Updated on June 04, 2022

Comments

  • H.tay
    H.tay almost 2 years

    Hi i'm trying to paginate my view and i have this following code in my controller for the pagination:

        public function membrevis()
          {
          $filter = isset($_GET['filter']) ? $_GET['filter'] : null;
          $query = DB::table('users')
            ->join('user_role', 'users.id', '=', 'user_role.user_id')
            ->join('roles', 'user_role.role_id', '=', 'roles.id')
            ->where('users.valid','=',0)
            ->select('users.*','roles.description');
            if ($filter != null) {
            $query->where('users.name','like','%'.$filter.'%')
                ->orWhere('roles.description','like','%'.$filter.'%');
            }
         $users = $query->get();
    
    
        $itemsPerPage = 8 ;
        $currentPage  = isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ? $_GET['page'] : 1;
        $urlPattern   = '/profilecontroller/membrevis?page=(:num)';
        $totalItems   = $users->count();
        $donner   = $users->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();
        $paginator = new  Paginator( $totalItems, $itemsPerPage, $currentPage, $urlPattern );
    
    return view('membre2',['users'=> $donner,'paginator'=> $paginator]);
    

    My view membre2.blade.php has the following code as well for pagination:

      <form>
     ....
      </form>
     <table>
    @foreach($users as $row)
    ....
    @endforeach
     </table>
    echo $paginator;
    

    But i' still get this error Method Illuminate\Support\Collection::offset does not exist. knowing i already installed "jasongrimes/paginator:~1.0" by CMD

    any help would be appreciated!

    • Mehdi Alipour
      Mehdi Alipour almost 6 years
      what is $users query in your controller? show your code completely.
    • Kyslik
      Kyslik almost 6 years
      Laravel 4 or Laravel 5, pick one.
    • Davit Zeynalyan
      Davit Zeynalyan almost 6 years
      before $donner code what is dd(get_class($user)) it is printed Illuminate\Support\Collection;
    • Sachin Aghera
      Sachin Aghera almost 6 years
      May be offset value set to 0
    • H.tay
      H.tay almost 6 years
      @MehdiAlipour I just update my post with the full code of my controller profilecontroller.php
    • H.tay
      H.tay almost 6 years
      Hi @Davit it returns : "Illuminate\Support\Collection"
    • H.tay
      H.tay almost 6 years
      @Kyslik I'm sorry I 'm working basically on Laravel 5 but i think this has nothing to do with which version of Laravel
    • Davit Zeynalyan
      Davit Zeynalyan almost 6 years
      see my edited answer
  • H.tay
    H.tay almost 6 years
    So you think my code should change to: ** $donner = $query->get()->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();** ??
  • Davit Zeynalyan
    Davit Zeynalyan almost 6 years
    when you use $query->get() it return instance of Illuminate\Support\Collection class. the ->get() method must be used the end of query
  • H.tay
    H.tay almost 6 years
    Hi that error is finally gone! Thank you man! But there is still one problem the echo $paginator; call in my view doesn't eem to work, it is shown as it is in my view and doesnt return the pagination! do you know hwo to fix this?
  • Davit Zeynalyan
    Davit Zeynalyan almost 6 years
    I dont used Paginator class. in this case i can not help you.
  • Davit Zeynalyan
    Davit Zeynalyan almost 6 years
    see official documentation https://laravel.com/api/5.5/Illuminate/Pagination/Paginator.‌​html it must be helpful for you