Laravel Arr only() function Example

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


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']);
        // ['name' => 'Desk', 'price' => 100]


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('users', [UserController::class,'index']);


Array ( [name] => Desk [price] => 100 )

I hope you understand laravel array only() function and it can help you…

Read Also :  Laravel Carbon Get All Months Between Two Dates Example