How to call variable inside collection – Laravel?

Laravel Framework provides us with a collection class. It is a convenient, and fluent wrapper that makes it easier to work with arrays of data. The collect helper method can be used to create instance for a new collection from the array. You can then run the strtoupper function on each element of the array and perform any function on it. Let us look through on how to call variable inside collection.

The Collection class comes from the Laravel Illuminate\Support\ classes. It can help chain methods that do fluent mapping and reduction of an array. They are immutable, i.e. each Collection method will return a completely new Collection instance.

Call variable inside Collection

$accessMe = ' is the current number';
$somevariable = collect([1, 2, 3, 4])->map(function($value) use ($accessMe) {
  return $value . $accessMe; 
}

Leave a Comment