TOPIC COLLECTION

Collections.

A collection of 8 practical ideas to make everyday Laravel work a little smoother.

08 TIPS
8 tips
01Use Groupby on Collections with Custom Callback FunctionIf you want to group result by some condition which isn’t a direct column in your database, you can do that by providing a closure function.Collections02Laravel Scopes Can Be Combined Using "Higher Order" Orwhere MethodFollowing example from the Docs. BeforeCollections03Multiple Collection Methods in a RowIf you query all results with - all() or - get() , you may then perform various Collection operations on the same result, it won’t query database every time.Collections04Calculate Sum with PaginationHow to calculate the sum of all records when you have only the PAGINATED collection? Do the calculation BEFORE the pagination, but from the same query.Collections05Serial No in Foreach Loop with PaginationWe can use foreach collection items index as serial no (SL) in pagination. it will solve the issue of next pages(?page=2&...) index count from continue.Collections06Higher Order Collection MessageCollections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. This example calculates the…Collections07Get an Existing Key or Insert a Value If It Doesn't Exist and Return the ValueIn Laravel 8.81 getOrPut method to Collections that simplifies the use-case where you want to either get an existing key or insert a value if it doesn't…Collections08Static Times MethodThe static times method creates a new collection by invoking the given closure a specified number of times.Collections