In this post, I explain laravel Arr first() function example. The laravel Arr first() function returns the first element of an array passing a given truth test.
Example : Laravel Arr::first() function
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Arr; class UserController extends Controller { public function index(){ $array = [100, 200, 300]; $first = Arr::first($array, function ($value, $key) { return $value >= 150; }); echo $first; } }
Output
200
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; Route::get('users', [UserController::class,'index']);
I hope you understand laravel Arr first() function and it can help you…