Skip to content

Commit

Permalink
Revert "formatting"
Browse files Browse the repository at this point in the history
This reverts commit 7f99b66.
  • Loading branch information
m777z authored Jun 8, 2019
1 parent ba42b17 commit c76cb0c
Showing 1 changed file with 183 additions and 55 deletions.
238 changes: 183 additions & 55 deletions eloquent-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,103 +33,231 @@ However, collections are much more powerful than arrays and expose a variety of
<a name="available-methods"></a>
## Available Methods

All Eloquent collections extend the base [Laravel collection](/docs/{{version}}/collections#available-methods) object; therefore, they inherit all of the powerful methods provided by the base collection class.
### The Eloquent Collection
`Illuminate\Database\Eloquent\Collection` provides a superset of methods to
aid with managing your model collection. Methods use `Model::getKey()` for manipulating
the collection. Most methods return a Collection of `Illuminate\Database\Eloquent\Collection`, however
some methods return a base Collection instance. Eloquent Collection methods are as follows:

In addition, the `Illuminate\Database\Eloquent\Collection` class provides a superset of methods to aid with managing your model collections. Most methods return `Illuminate\Database\Eloquent\Collection` instances; however, some methods return a base `Illuminate\Support\Collection` instance.

#### `contains($key, $operator = null, $value = null)`

The `contains` method may be used to determine if a given model instance is contained by the collection. This method accepts a primary key or a model instance:

$users->contains(1);

$users->contains(User::find(1));
#### `find($key)` {#collection-method .first-collection-method}

#### `diff($items)`
Returns the model found within the collection with the given key.
If `$key` is a model instance, `find` will attempt to return a model matching the primary
key. If `$key` is an array of keys, `find` will return all models which match
the `$keys` using `whereIn()`.

The `diff` method returns all of the models that are not present in the given collection:
$users = User::all();

$users->find(1);

use App\User;
#### `load($relations)`

$users = $users->diff(User::whereIn('id', [1, 2, 3])->get());
Eager load a set of relationships onto each model in the
collection.

#### `except($keys)`
$collection->load('users.comments');
$collection->load('users', 'admins');

The `except` method returns all of the models that do not have the given primary keys:

$users = $users->only([1, 2, 3]);
#### `loadMissing($relations)`

#### `find($key)` {#collection-method .first-collection-method}
Load any missing relationships onto each model in
the collection.

The `find` method finds a model that has a given primary key. If `$key` is a model instance, `find` will attempt to return a model matching the primary key. If `$key` is an array of keys, `find` will return all models which match the `$keys` using `whereIn()`:
$collection->loadMissing('users.comments');
$collection->loadMissing('admins');

$users = User::all();
#### `add($item)`

$user = $users->find(1);
Appends the supplied `$item` to the `items` property within the collection.

#### `fresh($with = [])`
$result = User::find(1);
$result->add(User::find(2));
#### `contains($key, $operator = null, $value = null)`

The `fresh` method retrieves a fresh instance of each model in the collection from the database. In addition, any specified relationships will be eager loaded:
Provides a convenient way of checking if a `Model` instance
is within the collection.

$users = $users->fresh();
If all three parameters are passed, this method behaves similar to `Illuminate\Support\Collection`.

$users = $users->fresh('comments');
If only `$key` is passed, the following occurs:

#### `intersect($items)`
If `$key` is an instance of a model, `contains`
will compare the `$key` to each `Model` in the collection using `Model::is()`.
Otherwise `$key` will be compared to each `Model`'s `Model::getKey()`.

The `intersect` method returns all of the models that are also present in the given collection:
$users->contains(1);
// True

$users->contains(User::find(1));
// True

use App\User;
#### `modelKeys`

$users = $users->intersect(User::whereIn('id', [1, 2, 3])->get());
`modelKeys` returns all primary keys found within the collection. Uses: `Model::getKey()`.

#### `load($relations)`
$users->modelKeys();
// [1,2,3,4,5]

The `load` method eager loads the given relationships for all models in the collection:
#### `fresh($with)`

$users->load('comments', 'posts');
Loads a fresh instance of each `Model` in the collection from the database with the specified
relationships.

$users->load('comments.author');
$users->fresh();

$users->fresh('comments');

#### `getDictionary($items = null)`

#### `loadMissing($relations)`
Return a dictionary of models key'd by the primary key. If `$items` is passed, it will attempt
to transform them into a dictionary'd array.

The `loadMissing` method eager loads the given relationships for all models in the collection if the relationships are not already loaded:
$users = User::all();
$users->getDictionary()
{
1 => User,
2 => User
}

#### `makeVisible($attribues)`

$users->loadMissing('comments', 'posts');
Make attributes which are typically hidden visible on each model in the collection.

$users->loadMissing('comments.author');
#### `makeHidden($attributes)`

#### `modelKeys`
Make attributes which are typically visible hidden on each model in the collection.

The `modelKeys` method returns the primary keys for all models in the collection:
#### `unique($key = null, $strict = false)`

$users->modelKeys();
Return only unique items from the collection. Unique items are determined
by the primary key using: `Model::getKey()`.

// [1, 2, 3, 4, 5]

#### `makeVisible($attributes)`
#### `only($keys)`

The `makeVisible` method makes visible attributes that are typically "hidden" on each model in the collection:
Return only the models which contain the specified primary keys `$keys`.

$users = $users->makeVisible(['address', 'phone_number']);
#### `except($keys)`

#### `makeHidden($attributes)`
Return only the models which DO NOT contain the specified primary keys `$keys`.

The `makeHidden` method hides attributes that are typically "visible" on each model in the collection:
#### `intersect($items)`

$users = $users->makeHidden(['address', 'phone_number']);
Return all models from `$items` which are also within the current collection.

#### `only($keys)`
#### `diff($items)`

The `only` method returns all of the models that have the given primary keys:
Return all models from `$items` which are NOT within the current collection.

$users = $users->only([1, 2, 3]);
### The Base Collection

#### `unique($key = null, $strict = false)`
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:

The `unique` method returns all of the unique models in the collection. Any models of the same type with the same primary key as another model in the collection are removed.
<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;
}

$users = $users->unique();
#collection-method-list a {
display: block;
}
</style>

<div id="collection-method-list" markdown="1">

[all](/docs/{{version}}/collections#method-all)
[average](/docs/{{version}}/collections#method-average)
[avg](/docs/{{version}}/collections#method-avg)
[chunk](/docs/{{version}}/collections#method-chunk)
[collapse](/docs/{{version}}/collections#method-collapse)
[combine](/docs/{{version}}/collections#method-combine)
[concat](/docs/{{version}}/collections#method-concat)
[contains](/docs/{{version}}/collections#method-contains)
[containsStrict](/docs/{{version}}/collections#method-containsstrict)
[count](/docs/{{version}}/collections#method-count)
[crossJoin](/docs/{{version}}/collections#method-crossjoin)
[dd](/docs/{{version}}/collections#method-dd)
[diff](/docs/{{version}}/collections#method-diff)
[diffKeys](/docs/{{version}}/collections#method-diffkeys)
[dump](/docs/{{version}}/collections#method-dump)
[each](/docs/{{version}}/collections#method-each)
[eachSpread](/docs/{{version}}/collections#method-eachspread)
[every](/docs/{{version}}/collections#method-every)
[except](/docs/{{version}}/collections#method-except)
[filter](/docs/{{version}}/collections#method-filter)
[first](/docs/{{version}}/collections#method-first)
[flatMap](/docs/{{version}}/collections#method-flatmap)
[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)
[isNotEmpty](/docs/{{version}}/collections#method-isnotempty)
[keyBy](/docs/{{version}}/collections#method-keyby)
[keys](/docs/{{version}}/collections#method-keys)
[last](/docs/{{version}}/collections#method-last)
[map](/docs/{{version}}/collections#method-map)
[mapInto](/docs/{{version}}/collections#method-mapinto)
[mapSpread](/docs/{{version}}/collections#method-mapspread)
[mapToGroups](/docs/{{version}}/collections#method-maptogroups)
[mapWithKeys](/docs/{{version}}/collections#method-mapwithkeys)
[max](/docs/{{version}}/collections#method-max)
[median](/docs/{{version}}/collections#method-median)
[merge](/docs/{{version}}/collections#method-merge)
[min](/docs/{{version}}/collections#method-min)
[mode](/docs/{{version}}/collections#method-mode)
[nth](/docs/{{version}}/collections#method-nth)
[only](/docs/{{version}}/collections#method-only)
[pad](/docs/{{version}}/collections#method-pad)
[partition](/docs/{{version}}/collections#method-partition)
[pipe](/docs/{{version}}/collections#method-pipe)
[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)
[some](/docs/{{version}}/collections#method-some)
[sort](/docs/{{version}}/collections#method-sort)
[sortBy](/docs/{{version}}/collections#method-sortby)
[sortByDesc](/docs/{{version}}/collections#method-sortbydesc)
[splice](/docs/{{version}}/collections#method-splice)
[split](/docs/{{version}}/collections#method-split)
[sum](/docs/{{version}}/collections#method-sum)
[take](/docs/{{version}}/collections#method-take)
[tap](/docs/{{version}}/collections#method-tap)
[toArray](/docs/{{version}}/collections#method-toarray)
[toJson](/docs/{{version}}/collections#method-tojson)
[transform](/docs/{{version}}/collections#method-transform)
[union](/docs/{{version}}/collections#method-union)
[unique](/docs/{{version}}/collections#method-unique)
[uniqueStrict](/docs/{{version}}/collections#method-uniquestrict)
[unless](/docs/{{version}}/collections#method-unless)
[values](/docs/{{version}}/collections#method-values)
[when](/docs/{{version}}/collections#method-when)
[where](/docs/{{version}}/collections#method-where)
[whereStrict](/docs/{{version}}/collections#method-wherestrict)
[whereIn](/docs/{{version}}/collections#method-wherein)
[whereInStrict](/docs/{{version}}/collections#method-whereinstrict)
[whereNotIn](/docs/{{version}}/collections#method-wherenotin)
[whereNotInStrict](/docs/{{version}}/collections#method-wherenotinstrict)
[zip](/docs/{{version}}/collections#method-zip)

</div>

<a name="custom-collections"></a>
## Custom Collections
Expand Down

0 comments on commit c76cb0c

Please sign in to comment.