collection of keys – Laravel Collection keys() Method Example

collection of keys : Need to create collection of keys with multiple values (Laravel 7.x) & laravel collection map with keys Examples.

collection of keys – laravel collection map with keys

A collection of keys is called a​ The keys() method will help to create new collection of collection keys.

Syntax: collection of keys

$collecton->keys();

Example 1: collection of keys

public function index()
{
    $collection = collect([
            "first" => ["id"=>1, "name"=>"Virat", "role"=>"Manager"],
            "second" => ["id"=>2, "name"=>"Ridham", "role"=>"Staff"],
            "third" => ["id"=>3, "name"=>"Pankaj", "role"=>"User"],
        ]);
  
    $result = $collection->keys();
   
    dd($result);
  
}

Result

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => first
            [1] => second
            [2] => third
        )
)

I hope you get an idea about collection of keys.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment