MrDalmosh's avatar

Pagination in Laravel that is connected to Woocommerce site

As the title says I'm trying to create pagination in Laravel that gets its data about the products from a Woocommerce site. Laravel is connected to that site via codexshare/laravel-woocommerce package. When i try to use paginate it returns all of the data about the pages just without the links and the method links() doesn't work it just says that the method doesn't exist (BadMethodCall). Underneath I'll put the route file, the part of the controller that sends the data to the view and the view itself in which i dump the file that was sent by the controller. I should also point out I'm using Laravel 9.

Controller

public function index()
    {   
	  
 $svi_proizvodi = Product::paginate(15);

        return view('vendor.pagination.index')->with('svi_proizvodi', $svi_proizvodi);

    }​

View

<tbody name="proiz" id="proiz">
            @dd($svi_proizvodi)
        </tbody>

This is what is shown in View

Illuminate\Support\Collection {#1539 ▼
  #items: array:2 [▼
    "meta" => array:7 [▼
      "total_results" => 1531
      "total_pages" => 103
      "current_page" => 1
      "previous_page" => null
      "next_page" => 2
      "first_page" => 1
      "last_page" => 1531
    ]
    "data" => Illuminate\Support\Collection {#1538 ▶}
  ]
  #escapeWhenCastingToString: false
}
0 likes
5 replies
Nihir's avatar

Hi @mrdalmosh did you try this method?

 $svi_proizvodi = Product::paginate(15);
  return view('vendor.pagination.index',compact('svi_proizvodi');

in controller and in view file {{ $svi_proizvodi->links() }} or {{ $svi_proizvodi->onEachSide(5)->links() }} and your view is look like this:

@foreach($svi_proizvodi  as $row)
	{{$row->yourfields}}
@endforeach

after the endforeach table completion you have to use one of this method {{ $svi_proizvodi->links() }} or {{ $svi_proizvodi->onEachSide(5)->links() }}

I used this in all projects you have to try this

MrDalmosh's avatar

@Nihir Unfortunately it returns Method Illuminate\Support\Collection::links does not exist.

Nihir's avatar

Hi,@MrDalmosh sorry that not working for you you can try this

{!! $svi_proizvodi->withQueryString()->links('pagination::bootstrap-5') !!}

you have to put in the after the @endforeach function you can try this method, and I think it works for you and you don't forget to apply it to $svi_proizvodi = Product::paginate(15); this in your controller

MrDalmosh's avatar

@Nihir thank you for your help i've found something that should work for now

Please or to participate in this conversation.