Laravel 5.4
Awesome Laravel
- Awesome Laravel (Chirag Gude)
Prologue
- Release Notes
- Upgrade Guide
Getting Started
- Installation
- Configuration
- Directory Structure
- Laravel Homestead
- valet
Architecture Concepts
- Request Lifecycle
- Service Container
- Service Providers
- Facades
The Basics
- Routing
- Errors & Logging
- Middleware
- CSRF Protection
- Controllers
- HTTP Requests
- HTTP Responses
- Views
- HTTP Session
- Validation
Frontend
- Blade Templates
- Localization
- JavaScript & CSS Scaffolding
- Compiling Assets (Laravel Mix)
Security
- Authentication
- API Authentication (Passport)
- Authorization
- Encryption
- Hashing
- Resetting Passwords
Digging Deeper
- Artisan Console
- Queues
- Package Development
- Task Scheduling
- Broadcasting
- Cache
- Collections
- Events
- File Storage
- helpers
- Notifications
Database
- Database Getting Started
- Database Query Builder
- Database Pagination
- Database Migrations
- Database Seeding
- Redis
Eloquent ORM
- Eloquent Getting Started
- Eloquent Relationships
- Eloquent Collections
- Eloquent Mutators
- Eloquent Serialization
Testing
- Testing Getting Started
- HTTP Tests
- Browser Tests (Laravel Dusk)
- Database Testing
- Mocking
- redirect
Official Packages
- Laravel Cashier
- Envoy Task Runner
- Laravel Scout
Eloquent Collections
Introduction
All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection
object, including results retrieved via the get
method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.
Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:
However, collections are much more powerful than arrays and expose a variety of map / reduce operations that may be chained using an intuitive interface. For example, let’s remove all inactive models and gather the first name for each remaining user:
While most Eloquent collection methods return a new instance of an Eloquent collection, the pluck
, keys
, zip
, collapse
, flatten
and flip
methods return a base collection instance. Likewise, if a map
operation returns a collection that does not contain any Eloquent models, it will be automatically cast to a base collection.
Available Methods
The Base Collection
All Eloquent collections extend the base Laravel collection object; therefore, they inherit all of the powerful methods provided by the base collection class:
all
average
avg
chunk
collapse
combine
contains
containsStrict
count
diff
diffKeys
each
every
except
filter
first
flatMap
flatten
flip
forget
forPage
get
groupBy
has
implode
intersect
isEmpty
isNotEmpty
keyBy
keys
last
map
mapWithKeys
max
median
merge
min
mode
nth
only
partition
pipe
pluck
pop
prepend
pull
push
put
random
reduce
reject
reverse
search
shift
shuffle
slice
sort
sortBy
sortByDesc
splice
split
sum
take
tap
toArray
toJson
transform
union
unique
uniqueStrict
values
when
where
whereStrict
whereIn
whereInStrict
whereNotIn
whereNotInStrict
zip
Custom Collections
If you need to use a custom Collection
object with your own extension methods, you may override the newCollection
method on your model:
|
Once you have defined a newCollection
method, you will receive an instance of your custom collection anytime Eloquent returns a Collection
instance of that model. If you would like to use a custom collection for every model in your application, you should override the newCollection
method on a base model class that is extended by all of your models.