Method links does not exist error

When using voyager with(‘translations’) and paginate together, this error occurs when creating pagination links.

Original:

//Controller
$news_list = News::list_data($latest->id, 1);
$data['news_list'] = $news_list->translate(App()->getLocale());

//View
{{ $news_list->links() }}

Solution:

//Controller
$news_list = News::list_data($latest->id, 1);
$data['news_list'] = $news_list;

//View
@foreach($news_list as $news)
    <h2>{!!nl2br($news->getTranslatedAttribute('title', $locale))!!}</h2>
@endforeach

{{ $news_list->links() }}

Solution 2:

    public function products(Request $req)
    {
        $data = $this->init_data();

        $products = Product::orderBy('created_at')
            ->paginate(2);
        //dd($page_data);

        foreach ($products as $key => $product) {
            $product->text1 = $product->getTranslatedAttribute('text1', App()->getLocale(), 'zhtw');
        }

        $data['products'] = $products;
        
        return view('products', $data);
    }

 

Reference:

  1. https://laracasts.com/discuss/channels/laravel/links-method-doesnt-exist-after-sortbydesc-on-hasmany