Laravel Collection Remove Item by Value Example - TechvBlogs

Laravel Collection Remove Item by Value Example

Explore the simplicity of removing items by value in Laravel Collections. Uncover efficient techniques to streamline your code and enhance your Laravel development experience with this insightful example.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

4 months ago

TechvBlogs - Google News

This comprehensive guide will instruct you on removing items by value in Laravel collections. I'll explain a simple method for removing items by value in a Laravel collection. You'll learn how to delete items by value in Laravel collections, and this post will provide a straightforward example of removing items by value in a Laravel collection.

In this example, we'll employ the reject() method to eliminate an element by value in a Laravel collection.

The reject method in Laravel collections serves to filter a collection by excluding elements that satisfy a specified condition. It produces a new collection comprising all elements that do not meet the given condition. The basic syntax for using the reject method is as follows:

Here's an example of how you can utilize the reject() method in Laravel:

Laravel Collection Remove Item by Value Example 1:

Below is an example of controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect(['Smit', 'Suresh', 'Vishal', 'Bhavik']);
  
        $valueToRemove = 'Vishal';
        $filteredCollection = $collection->reject(function ($item) use ($valueToRemove) {
            return $item === $valueToRemove;
        });
    
        dd($filteredCollection->toArray());
    }
}

Output:

Array
(
    [0] => Smit
    [1] => Suresh
    [2] => Bhavik
)

Laravel Collection Remove Item by Value Example 2:

 Below is an example of controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect([
            ['id' => 1, 'name' => 'Smit', 'email' => '[email protected]'],
            ['id' => 2, 'name' => 'Suresh', 'email' => '[email protected]'],
            ['id' => 3, 'name' => 'Bhavik', 'email' => '[email protected]'],
        ]);
  
        $valueToRemove = 'Bhavik';
        $filteredCollection = $collection->reject(function ($item) use ($valueToRemove) {
            return $item['name'] === $valueToRemove;
        });
    
        dd($filteredCollection->toArray());
    }
}

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Smit
            [email] => [email protected]
        )
    [2] => Array
        (
            [id] => 3
            [name] => Suresh
            [email] => [email protected]
        )
)

Thank you! If you have any more questions or if there's anything else I can assist you with, feel free to let me know.

Comments (0)

Comment


Note: All Input Fields are required.