The Laravel Arr only() function example returns only the specified key / value pairs from the given array. Laravel array only() function is use when you get only the specified key / value pairs from array. Also, We will show how to use Arr::only() function in laravel.
Example: Laravel Arr::only() 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 = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); print_r($slice); // ['name' => 'Desk', 'price' => 100] } }
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; Route::get('users', [UserController::class,'index']);
Output
Array ( [name] => Desk [price] => 100 )
I hope you understand laravel array only() function and it can help you…