Remove NULL or Empty Array from Laravel Resource Collection

Sovary November 23, 2022 604
2 minutes read

Today we will learn how to take out empty or null items from a collection in Laravel application. We will show you how to delete empty item from collection data using simple method in Laravel. In the below example, we will guide you how to remove empty values from data collection in Laravel, so you will have only valid data in collection.

With the sample example below you can apply with Laravel 6, Laravel 7, Laravel 8, Laravel 9 as well. Let's see below example.

How to Delete Empty Array in Collection

  • Step 1 - Install Laravel
  • Step 2 - Create Controller File
  • Step 3 - Add Route
  • Step 4 - Start Laravel Server

Step 1 - Install Laravel

First thing to do, you have to download Laravel project to your local machine, so you have to connect to internet and installed via composer. You may read how to configure database connection and installation in the article or just follow this article.

Run below command to download Laravel project.

composer create-project laravel/laravel blog

Step 2 - Create Controller File

Now we will create controller file by following command to implement code and create a function

php artisan make:controller TestController

Open file app -> Http -> Controllers -> TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class TestController extends Controller
{
    
    public function index()
    {
        $col= new Collection([
            ['Data0'],
            ['Data1'],
            null,
            [],
            ]);

        $temp= $col->filter();
        dd($temp);
    }

}

Step 3 - Add Route

Next, we are going to adding a route which is point to controller and method should required. 

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TestController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::get('/', [TestController::class,'index']);

Step 4 - Start Laravel Server

We will skip step creating blade file (view), now we ready for testing the application. By following command to run Laravel server.

php artisan serve

Then launch your browser with URL http://localhost:8000/ to see the collection result. 

Illuminate\Database\Eloquent\Collection {#1863 ▼
  #items: array:2 [▼
    0 => array:1 [▼
      0 => "Data0"
    ]
    1 => array:1 [▼
      0 => "Data1"
    ]
  ]
}

Hope this article help you to filter empty element from collection data. Have a nice day!

You may also like...

Laravel  PHP  Laravel 9  Laravel Collections 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him