Laravel - Collections - Method First Where

The firstWhere method 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]
	

You may also call the firstWhere method with a comparison operator:

    
    $collection->firstWhere('age', '>=', 18);
    
    // ['name' => 'Diego', 'age' => 23]
	

Like the where method, you may pass one argument to the firstWhere method. In this scenario, the firstWhere method will return the first item where the given item key's value is "truthy":

    
    $collection->firstWhere('age');
    
    // ['name' => 'Linda', 'age' => 14]