• Barajar
    Activar
    Desactivar
  • Alphabetizar
    Activar
    Desactivar
  • Frente Primero
    Activar
    Desactivar
  • Ambos lados
    Activar
    Desactivar
  • Leer
    Activar
    Desactivar
Leyendo...
Frente

Cómo estudiar sus tarjetas

Teclas de Derecha/Izquierda: Navegar entre tarjetas.tecla derechatecla izquierda

Teclas Arriba/Abajo: Colvea la carta entre frente y dorso.tecla abajotecla arriba

Tecla H: Muestra pista (3er lado).tecla h

Tecla N: Lea el texto en voz.tecla n

image

Boton play

image

Boton play

image

Progreso

1/88

Click para voltear

88 Cartas en este set

  • Frente
  • Atrás
contains($key, $operator = null, $value = null)
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));
diff($items)
Returns all of the models that are not present in the given collection:
$users = $users->diff(User::whereIn('id', [1, 2, 3])->get());
except($keys)
Returns all of the models that do not have the given primary keys:
find($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():
$user = $users->find(1);
fresh($with = [])
etrieves a fresh instance of each model in the collection from the database. In addition, any specified relationships will be eager loaded:
$users = $users->fresh();
$users = $users->fresh('comments');
intersect($items)
returns all of the models that are also present in the given collection:
$users = $users->intersect(User::whereIn('id', [1, 2, 3])->get());
load($relations)
The load method eager loads the given relationships for all models in the collection:
$users->load('comments', 'posts');
$users->load('comments.author');
loadMissing($relations)
The loadMissing method eager loads the given relationships for all models in the collection if the relationships are not already loaded:
$users->loadMissing('comments', 'posts');
$users->loadMissing('comments.author');
modelKeys
The modelKeys method returns the primary keys for all models in the collection:
$users->modelKeys();
makeVisible($attributes)
The makeVisible method makes visible attributes that are typically "hidden" on each model in the collection:
$users = $users->makeVisible(['address', 'phone_number']);
makeHidden($attributes)
The makeHidden method hides attributes that are typically "visible" on each model in the collection:
$users = $users->makeHidden(['address', 'phone_number']);
only($keys)
Returns all of the models that have the given primary keys:
$users = $users->only([1, 2, 3]);
unique($key = null, $strict = false)
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.
$users = $users->unique();
all()
Returns the underlying array represented by the collection:
collect([1, 2, 3])->all();
// [1, 2, 3]
average()
avg()
Returns the average value of a given key:
$average = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->avg('foo');
// 20
$average = collect([1, 1, 2, 4])->avg();
// 2
chunk()
The chunk method breaks the collection into multiple, smaller collections of a given size:
$collection = collect([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk(4);
$chunks->toArray();
// [[1, 2, 3, 4], [5, 6, 7]]
collapse()
The collapse method collapses a collection of arrays into a single, flat collection:
$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
$collapsed = $collection->collapse();
$collapsed->all();
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
combine()
The combine method combines the values of the collection, as keys, with the values of another array or collection:
$collection = collect(['name', 'age']);
$combined = $collection->combine(['George', 29]);
$combined->all();
// ['name' => 'George', 'age' => 29]
concat()
The concat method appends the given array or collection values onto the end of the collection:
$collection = collect(['John Doe']);
$concatenated = $collection->concat(['Jane Doe'])->concat(['name' => 'Johnny Doe']);
$concatenated->all();
// ['John Doe', 'Jane Doe', 'Johnny Doe']
contains()
The contains method determines whether the collection contains a given item:
$collection = collect(['name' => 'Desk', 'price' => 100]);
$collection->contains('Desk');
// true
$collection->contains('New York');
// false
containsStrict()
This method has the same signature as the contains method; however, all values are compared using "strict" comparisons.
count()
The count method returns the total number of items in the collection:
$collection = collect([1, 2, 3, 4]);
$collection->count();
// 4
countBy()
The countBy method counts the occurences of values in the collection. By default, the method counts the occurrences of every element:
$collection = collect([1, 2, 2, 2, 3]);
$counted = $collection->countBy();
$counted->all();
// [1 => 1, 2 => 3, 3 => 1]
However, you pass a callback to the countBy method to count all items by a custom value:
$collection = collect(['alice@gmail.com', 'bob@yahoo.com', 'carlos@gmail.com']);
$counted = $collection->countBy(function ($email) {
return substr(strrchr($email, "@"), 1);
});
$counted->all();
// ['gmail.com' => 2, 'yahoo.com' => 1]
crossJoin()
The crossJoin method cross joins the collection's values among the given arrays or collections, returning a Cartesian product with all possible permutations:
$collection = collect([1, 2]);
$matrix = $collection->crossJoin(['a', 'b']);
$matrix->all();
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
dd()
The dd method dumps the collection's items and ends execution of the script:
$collection = collect(['John Doe', 'Jane Doe']);
$collection->dd();
/*
Collection {
#items: array:2 [
0 => "John Doe"
1 => "Jane Doe"
]
}
*/
If you do not want to stop executing the script, use the dump method instead.
each()
The each method iterates over the items in the collection and passes each item to a callback:
$collection->each(function ($item, $key) {});
If you would like to stop iterating through the items, you may return false from your callback:
eachSpread()
The eachSpread method iterates over the collection's items, passing each nested item value into the given callback:
$collection = collect([['John Doe', 35], ['Jane Doe', 33]]);
$collection->eachSpread(function ($name, $age) {});
every()
The every method may be used to verify that all elements of a collection pass a given truth test:
collect([1, 2, 3, 4])->every(function ($value, $key) {
return $value > 2;
});
// false
If the collection is empty, every will return true
filter()
The filter method filters the collection using the given callback, keeping only those items that pass a given truth test:
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [3, 4]
first()
The first method returns the first element in the collection that passes a given truth test:
collect([1, 2, 3, 4])->first(function ($value, $key) {
return $value > 2;
});
// 3
firstWhere()
Returns the first element in the collection with the given key / value pair:
$collection = collect([
['name' => 'Regena', 'age' => null],
['name' => 'Linda', 'age' => 14],
['name' => 'Diego', 'age' => 23],
['name' => 'Linda', 'age' => 84],
]);
$collection->firstWhere('name', 'Linda');
// ['name' => 'Linda', 'age' => 14]

$collection->firstWhere('age', '>=', 18);
// ['name' => 'Diego', 'age' => 23]
flatMap()
The flatMap method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items. Then, the array is flattened by a level:
flip()
The flip method swaps the collection's keys with their corresponding values:
forget()
Removes an item from the collection by its key:
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$collection->forget('name');
$collection->all();
// ['framework' => 'laravel']
forPage()
etorna una nueva colección conteniendo los elementos que estrían presentes en una página concreta. El método acepta el número de página como primer argumento y el número de elementos a mostrar por página como segundo parámetro:
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunk = $collection->forPage(2, 3);
$chunk->all();
// [4, 5, 6]
get()
The get method returns the item at a given key. If the key does not exist, null is returned:
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$value = $collection->get('name');
// taylor
groupBy()
The groupBy method groups the collection's items by a given key:
$collection = collect([
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
['account_id' => 'account-x11', 'product' => 'Desk'],
]);
$grouped = $collection->groupBy('account_id');
$grouped->toArray();
/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/
has()
Determines if a given key exists in the collection:
$collection = collect(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->has('product');
// true
$collection->has(['product', 'amount']);
// true
$collection->has(['amount', 'price']);
// false
implode()
The implode method joins the items in a collection. Its arguments depend on the type of items in the collection. If the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values:
$collection = collect([
['account_id' => 1, 'product' => 'Desk'],
['account_id' => 2, 'product' => 'Chair'],
]);
$collection->implode('product', ', ');
// Desk, Chair
If the collection contains simple strings or numeric values, pass the "glue" as the only argument to the method:
collect([1, 2, 3, 4, 5])->implode('-');
// '1-2-3-4-5'
isEmpty()
The isEmpty method returns true if the collection is empty; otherwise, false is returned:
isNotEmpty()
The isNotEmpty method returns true if the collection is not empty; otherwise, false is returned:
join
The join method joins the collection's values with a string:
collect(['a', 'b', 'c'])->join(', '); // 'a, b, c'
collect(['a', 'b', 'c'])->join(', ', ', and '); // 'a, b, and c'
collect(['a', 'b'])->join(', ', ' and '); // 'a and b'
collect(['a'])->join(', ', ' and '); // 'a'
collect([])->join(', ', ' and '); // ''
keyBy()
The keyBy method keys the collection by the given key. If multiple items have the same key, only the last one will appear in the new collection:
$collection = collect([
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
]);
$keyed = $collection->keyBy('product_id');
$keyed->all();
/*
[
'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]
*/
keys()
Returns all of the collection's keys
last()
Returns the last element in the collection that passes a given truth test.
You may also call the last method with no arguments to get the last element in the collection. If the collection is empty, null is returned.
make()
The static make method creates a new collection instance.
map()
iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items.
mapSpread()
The mapSpread method iterates over the collection's items, passing each nested item value into the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:
$collection = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunks = $collection->chunk(2);
$sequence = $chunks->mapSpread(function ($even, $odd) {
return $even + $odd;
});
$sequence->all();
// [1, 5, 9, 13, 17]
max()
The max method returns the maximum value of a given key:
$max = collect([['foo' => 10], ['foo' => 20]])->max('foo');
// 20
$max = collect([1, 2, 3, 4, 5])->max();
// 5
merge()
The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection:
$collection = collect(['product_id' => 1, 'price' => 100]);
$merged = $collection->merge(['price' => 200, 'discount' => false]);
$merged->all();
// ['product_id' => 1, 'price' => 200, 'discount' => false]
If the given items's keys are numeric, the values will be appended to the end of the collection
min()
The min method returns the minimum value of a given key
pipe()
The pipe method passes the collection to the given callback and returns the result:
pluck()
The pluck method retrieves all of the values for a given key:
$collection = collect([
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
]);
$plucked = $collection->pluck('name');
$plucked->all();
// ['Desk', 'Chair']
You may also specify how you wish the resulting collection to be keyed:
$plucked = $collection->pluck('name', 'product_id');
$plucked->all();
// ['prod-100' => 'Desk', 'prod-200' => 'Chair']
pop()
The pop method removes and returns the last item from the collection
prepend()
The prepend method adds an item to the beginning of the collection.
You may also pass a second argument to set the key of the prepended item.
pull()
The pull method removes and returns an item from the collection by its key
push()
The push method appends an item to the end of the collection
put()
The put method sets the given key and value in the collection:
$collection = collect(['product_id' => 1, 'name' => 'Desk']);
$collection->put('price', 100);
$collection->all();
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]
random()
The random method returns a random item from the collection.
You may optionally pass an integer to random to specify how many items you would like to randomly retrieve.
reduce()
reduces the collection to a single value, passing the result of each iteration into the subsequent iteration
$collection = collect([1, 2, 3]);
$total = $collection->reduce(function ($carry, $item) {
return $carry + $item;
});
// 6
The value for $carry on the first iteration is null; however, you may specify its initial value by passing a second argument to reduce:
$collection->reduce(function ($carry, $item) {
return $carry + $item;
}, 4);
// 10
reject()
The reject method filters the collection using the given callback. The callback should return true if the item should be removed from the resulting collection:
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->reject(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [1, 2]
reverse()
The reverse method reverses the order of the collection's items, preserving the original keys
search()
The search method searches the collection for the given value and returns its key if found. If the item is not found, false is returned.
The search is done using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass true as the second argument to the method
shift()
The shift method removes and returns the first item from the collection:
shuffle()
The shuffle method randomly shuffles the items in the collection
slice()
The slice method returns a slice of the collection starting at the given index:
If you would like to limit the size of the returned slice, pass the desired size as the second argument to the method:
sort()
The sort method sorts the collection. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes:
sortBy()
The sortBy method sorts the collection by the given key
splice()
The splice method removes and returns a slice of items starting at the specified index:
split()
The split method breaks a collection into the given number of groups:
$collection = collect([1, 2, 3, 4, 5]);
$groups = $collection->split(3);
$groups->toArray();
// [[1, 2], [3, 4], [5]]
sum()
The sum method returns the sum of all items in the collection:
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to sum:
In addition, you may pass your own callback to determine which values of the collection to sum:
take()
he take method returns a new collection with the specified number of items:
You may also pass a negative integer to take the specified amount of items from the end of the collection:
tap()
El método tap pasa la colección por un callback proporcionado, permitiendo "tocar/alterar" la colección en un punto concreto y hacer algo con sus elementos sin afectar a la colección en sí misma:
collect([2, 4, 3, 1, 5])
->sort()
->tap(function ($collection) {
Log::debug('Values after sorting', $collection->values()->toArray());
})
->shift();
// 1
times()
The static times method creates a new collection by invoking the callback a given amount of times:
$collection = Collection::times(10, function ($number) {
return $number * 9;
});
$collection->all();
// [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
toArray()
The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays
toJson()
The toJson method converts the collection into a JSON serialized string
transform()
The transform method iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:
Transform modifies the collection itself. If you wish to create a new collection instead, use the map method.
unless
El método unless ejecutará el callback dado a no ser que el primer argumento del método se resuelva a true:
$collection = collect([1, 2, 3]);
$collection->unless(true, function ($collection) {
return $collection->push(4);
});
$collection->unless(false, function ($collection) {
return $collection->push(5);
});
$collection->all();
// [1, 2, 3, 5]
unwrap()
Returns the collection's underlying items from the given value when applicable:
Collection::unwrap(collect('John Doe'));
// ['John Doe']
Collection::unwrap(['John Doe']);
// ['John Doe']
Collection::unwrap('John Doe');
// 'John Doe'
values()
Returns a new collection with the keys reset to consecutive integers:
when()
El método when ejecutará el callback proporcionado cuando el primer argumento se resuelva a true:
where()
The where method filters the collection by a given key / value pair:
whereBetween()
The whereBetween method filters the collection within a given range:
whereIn()
The whereIn method filters the collection by a given key / value contained within the given array:
whereInstanceOf()
The whereInstanceOf method filters the collection by a given class type:
whereNotBetween()
The whereNotBetween method filters the collection within a given range:
whereNotIn()
The whereNotIn method filters the collection by a given key / value not contained within the given array:
zip()
The zip method merges together the values of the given array with the values of the original collection at the corresponding index:
$collection = collect(['Chair', 'Desk']);
$zipped = $collection->zip([100, 200]);
$zipped->all();
// [['Chair', 100], ['Desk', 200]]