Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 17, 2015
1 parent 88b62c8 commit 6f22b85
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 481 deletions.
2 changes: 1 addition & 1 deletion database.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Database Basics
# Database: Getting Started

- [Configuration](#configuration)
- [Running Raw SQL Queries](#running-queries)
Expand Down
8 changes: 4 additions & 4 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
- [Testing](/docs/{{version}}/testing)
- [Validation](/docs/{{version}}/validation)
- Database
- [The Basics](/docs/{{version}}/database)
- [Getting Started](/docs/{{version}}/database)
- [Query Builder](/docs/{{version}}/queries)
- [Migrations / Schema](/docs/{{version}}/schema)
- [Seeding](/docs/{{version}}/seeds)
- [Migrations](/docs/{{version}}/migrations)
- [Seeding](/docs/{{version}}/seeding)
- Eloquent ORM
- [The Basics](/docs/{{version}}/eloquent-basics)
- [Getting Started](/docs/{{version}}/eloquent)
- [Relationships](/docs/{{version}}/eloquent-relationships)
- [Collections](/docs/{{version}}/eloquent-collections)
- [Mutators](/docs/{{version}}/eloquent-mutators)
Expand Down
144 changes: 86 additions & 58 deletions eloquent-collections.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,105 @@
# Eloquent Collections
# Eloquent: Collections

<a name="collections"></a>
## Collections
- [Introduction](#introduction)
- [Available Methods](#available-methods)
- [Custom Collections](#custom-collections)

All multi-result sets returned by Eloquent, either via the `get` method or a `relationship`, will return a collection object. This object implements the `IteratorAggregate` PHP interface so it can be iterated over like an array. However, this object also has a variety of other helpful methods for working with result sets.
<a name="introduction"></a>
## Introduction

#### Checking If A Collection Contains A Key
All multi-result sets returned by Eloquent are an instance 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](/docs/{{version}}/collections), so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.

For example, we may determine if a result set contains a given primary key using the `contains` method:
Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:

$roles = User::find(1)->roles;
$users = App\User::where('active', 1)->get();

if ($roles->contains(2))
{
//
foreach ($users as $user) {
echo $user->name;
}

Collections may also be converted to an array or JSON:
However, collections are much more powerful than arrays and expose a variety of map / reduce operations using an intuitive interface. For example, let's remove all inactive models and gather the first name for each remaining user:

$roles = User::find(1)->roles->toArray();
$users = App\User::where('active', 1)->get();

$roles = User::find(1)->roles->toJson();

If a collection is cast to a string, it will be returned as JSON:

$roles = (string) User::find(1)->roles;

#### Iterating Collections

Eloquent collections also contain a few helpful methods for looping and filtering the items they contain:

$roles = $user->roles->each(function($role)
{
//
$names = $users->reject(function ($user) {
return $user->active === false;
})
->map(function ($user) {
return $user->name;
});

#### Filtering Collections
<a name="available-methods"></a>
## Available Methods

When filtering collections, the callback provided will be used as callback for [array_filter](http:https://php.net/manual/en/function.array-filter.php).
### The Base Collection

$users = $users->filter(function($user)
{
return $user->isAdmin();
});

> **Note:** When filtering a collection and converting it to JSON, try calling the `values` function first to reset the array's keys.
#### Applying A Callback To Each Collection Object
All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/collections) object; therefore, they inherit all of the powerful methods provided by the base collection class:

$roles = User::find(1)->roles;

$roles->each(function($role)
{
//
});

#### Sorting A Collection By A Value

$roles = $roles->sortBy(function($role)
{
return $role->created_at;
});

$roles = $roles->sortByDesc(function($role)
{
return $role->created_at;
});

#### Sorting A Collection By A Value

$roles = $roles->sortBy('created_at');
<style>
#collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}

$roles = $roles->sortByDesc('created_at');
#collection-method-list a {
display: block;
}
</style>

<div id="collection-method-list" markdown="1">
[all](/docs/{{version}}/collections#method-all)
[chunk](/docs/{{version}}/collections#method-chunk)
[collapse](/docs/{{version}}/collections#method-collapse)
[contains](/docs/{{version}}/collections#method-contains)
[count](/docs/{{version}}/collections#method-count)
[diff](/docs/{{version}}/collections#method-diff)
[each](/docs/{{version}}/collections#method-each)
[filter](/docs/{{version}}/collections#method-filter)
[first](/docs/{{version}}/collections#method-first)
[flatten](/docs/{{version}}/collections#method-flatten)
[flip](/docs/{{version}}/collections#method-flip)
[forget](/docs/{{version}}/collections#method-forget)
[forPage](/docs/{{version}}/collections#method-forpage)
[get](/docs/{{version}}/collections#method-get)
[groupBy](/docs/{{version}}/collections#method-groupby)
[has](/docs/{{version}}/collections#method-has)
[implode](/docs/{{version}}/collections#method-implode)
[intersect](/docs/{{version}}/collections#method-intersect)
[isEmpty](/docs/{{version}}/collections#method-isempty)
[keyBy](/docs/{{version}}/collections#method-keyby)
[keys](/docs/{{version}}/collections#method-keys)
[last](/docs/{{version}}/collections#method-last)
[map](/docs/{{version}}/collections#method-map)
[merge](/docs/{{version}}/collections#method-merge)
[pluck](/docs/{{version}}/collections#method-pluck)
[pop](/docs/{{version}}/collections#method-pop)
[prepend](/docs/{{version}}/collections#method-prepend)
[pull](/docs/{{version}}/collections#method-pull)
[push](/docs/{{version}}/collections#method-push)
[put](/docs/{{version}}/collections#method-put)
[random](/docs/{{version}}/collections#method-random)
[reduce](/docs/{{version}}/collections#method-reduce)
[reject](/docs/{{version}}/collections#method-reject)
[reverse](/docs/{{version}}/collections#method-reverse)
[search](/docs/{{version}}/collections#method-search)
[shift](/docs/{{version}}/collections#method-shift)
[shuffle](/docs/{{version}}/collections#method-shuffle)
[slice](/docs/{{version}}/collections#method-slice)
[sort](/docs/{{version}}/collections#method-sort)
[sortBy](/docs/{{version}}/collections#method-sortby)
[sortByDesc](/docs/{{version}}/collections#method-sortbydesc)
[splice](/docs/{{version}}/collections#method-splice)
[sum](/docs/{{version}}/collections#method-sum)
[take](/docs/{{version}}/collections#method-take)
[toArray](/docs/{{version}}/collections#method-toarray)
[toJson](/docs/{{version}}/collections#method-tojson)
[transform](/docs/{{version}}/collections#method-transform)
[unique](/docs/{{version}}/collections#method-unique)
[values](/docs/{{version}}/collections#method-values)
[where](/docs/{{version}}/collections#method-where)
[whereLoose](/docs/{{version}}/collections#method-whereloose)
[zip](/docs/{{version}}/collections#method-zip)
</div>

#### Returning A Custom Collection Type

Expand Down
4 changes: 2 additions & 2 deletions eloquent-relationships.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Eloquent ORM: Relationships
# Eloquent: Relationships

- [Introduction](#introduction)
- [Defining Relationships](#defining-relationships)
Expand Down Expand Up @@ -406,7 +406,7 @@ You may also retrieve the owner of a polymorphic relation from the polymorphic m

$imageable = $photo->imageable;

The `imageable` relation on the `Photo` model will return either a `Staff` or `Order` instance, depending on which type of model owns the photo.
The `imageable` relation on the `Photo` model will return either a `Staff` or `Product` instance, depending on which type of model owns the photo.

<a name="many-to-many-polymorphic-relations"></a>
### Many To Many Polymorphic Relations
Expand Down
2 changes: 1 addition & 1 deletion eloquent.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Eloquent ORM: The Basics
# Eloquent: Getting Started

- [Introduction](#introduction)
- [Defining Models](#defining-models)
Expand Down
2 changes: 1 addition & 1 deletion queries.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Query Builder
# Database: Query Builder

- [Introduction](#introduction)
- [Selects](#selects)
Expand Down
Loading

0 comments on commit 6f22b85

Please sign in to comment.