Laravel 9 Collection Check If Key Exists Example Tutorial

Reading Time: 3 minutes
1,334 Views

Inside this article we will see the concept i.e Laravel 9 Collection Check If Key Exists Example. Article contains the classified information about Checking key existence in Laravel Collection. We will see very simple and basic concept which you can use and learn to check laravel collection key.

You can check a single key as well as many keys at a time as per you need from your collection. If you are wondering to look a solution to check collection keys that it exists or not then this article will help you a lot.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Check Single Key into Collection

We will consider a laravel controller and work into a collection.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $post = collect([
                    'id' => 1,
                    'name' => 'Sanjay Kumar',
                    'email' => 'sanjay_test_example@gmail.com'
                ]);
  
        if ($post->has('name')) {

            dd("Name key is exist on collection.");
        }
  
        dd("Name key doesn't exist on collection.");
    }
}

Output

Name key is exist on collection.

Example #2: Check Multiple Keys into Collection

We will consider a laravel controller and work into a collection.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $post = collect([
                    'id' => 1,
                    'name' => 'Sanjay Kumar',
                    'email' => 'sanjay_test_example@gmail.com'
                ]);
  
        if ($post->has(['name', 'email'])) {

            dd("Name and Email keys are exist on collection.");
        }
  
        dd("Name and Email keys don't exist on collection.");
    }
}

Output

Name and Email keys are exist on collection.

We hope this article helped you to Learn Laravel 9 Collection Check If Key Exists Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.