How to get the first element of a collection in illuminate/collections?

To get the first element of a collection in Illuminate/Collections, you can use the first method. Here's an example:

use Illuminate\Support\Collection; $collection = collect([1, 2, 3, 4, 5]); $firstElement = $collection->first(); echo $firstElement; // Output: 1

In this example, the collect function is used to create a collection with the elements [1, 2, 3, 4, 5]. The first method retrieves the first element of the collection, and it is assigned to the $firstElement variable. Finally, the first element is printed using echo.