简体   繁体   English

如何在 Laravel 8 的刀片中显示分页链接?

[英]How to display pagination links in a blade on Laravel 8?

I need to add pagination, but it seems complicated in my case, because I get data from database with paginate, but then I modify this data and when I call links() method on the blade, I get the following exception我需要添加分页,但在我的情况下似乎很复杂,因为我使用分页从数据库中获取数据,但随后我修改了这些数据,当我在刀片上调用links()方法时,出现以下异常
Method Illuminate\\Support\\Collection::links does not exist.方法 Illuminate\\Support\\Collection::links 不存在。
My code in the Controller method:我在 Controller 方法中的代码:

$transactionsByLastMonth = Transaction::where('created_at', '>=', Carbon::now()->subDays(30)->toDateTimeString())
    ->where('user_id', Auth::user()->id)
    ->with(['user', 'propertyFrom', 'propertyTo', 'paddockFrom', 'paddockTo', 'cattleTypeFrom', 'cattleTypeTo'])
    ->paginate(10);
        
$transactionsByDays = collect(TransactionResource::collection($transactionsByLastMonth))
    ->sortByDesc('created_at')
    ->groupBy(function($date) {
        return Carbon::parse($date['created_at'])->format('d');
    });

return view('user.reports.index', compact('transactionsByDays'));

Yes, a pagination limits my data to 10 rows, but due to I'm grouping data by days, my collection modifies itself and I can't use $transactionsByDays->links() method to show pagination.是的,分页将我的数据限制为 10 行,但由于我按天对数据进行分组,我的集合会自行修改,我无法使用$transactionsByDays->links()方法来显示分页。
If see dd($transactionsByDays) , it looks like:如果看到dd($transactionsByDays) ,它看起来像:

Illuminate\Support\Collection {#1381 ▼
  #items: array:3 [▼
    "01" => Illuminate\Support\Collection {#1019 ▼
      #items: array:7 [▼
        0 => array:16 [▶]
        1 => array:16 [▶]
        2 => array:16 [▶]
        3 => array:16 [▶]
        4 => array:16 [▶]
        5 => array:16 [▶]
        6 => array:16 [▶]
      ]
    }
    31 => Illuminate\Support\Collection {#1386 ▼
      #items: array:2 [▼
        0 => array:16 [▶]
        1 => array:16 [▶]
      ]
    }
    30 => Illuminate\Support\Collection {#1384 ▼
      #items: array:1 [▼
        0 => array:16 [▶]
      ]
    }
  ]
}

Therefore I need to paginate all arrays together which inside "01", 31, 30... How can I do it?因此,我需要将“01”、31、30 内的所有数组分页在一起......我该怎么做?
Maybe rewrite code above in the controller somehow?也许以某种方式在控制器中重写上面的代码?
The main aim is that I need to group data to every day according to my json-resource class.主要目的是我需要根据我的 json-resource 类对每天的数据进行分组。
Any ideas?有任何想法吗?

I've found a solution gyus.我找到了一个解决方案gyus。 I had to pass also a variable transactionsByLastMonth and use links() method over this.我还必须传递一个变量transactionsByLastMonth并在此之上使用links()方法。
In the meantime I use foreach on the my blade file over transactionsByDays variable.与此同时,我在我的刀片文件上通过transactionsByDays变量使用 foreach。 It's correctly works together cause it uses the same data from database, just in the first case its not filtered and not grouped and in the second one it is.它可以正确地协同工作,因为它使用来自数据库的相同数据,只是在第一种情况下它没有过滤也没有分组,而在第二种情况下它是。

return view('user.reports.index', compact('transactionsByLastMonth', 'transactionsByDays'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM