put method is used to add a new item to the collection with a given
put method modifies the original
Collection instance and returns a reference to it.
public function put( $key, $value );
The following code example highlights the usage of the
use Illuminate\Support\Collection; // Create a new collection instance. $collection = new Collection([ 'first' => 'I am first', 'second' => 'I am second' ]); // Put a new item in the collection. $collection->put('third', 'I am third');
After the above code has executed, the
$collection variable would contain a value similar to the following output:
object(Illuminate\Support\Collection) protected 'items' => array 'first' => string 'I am first' 'second' => string 'I am second' 'third' => string 'I am third'