Laravel Collection Remove Item by Value Example
Hi Dev,
This extensive guide will teach you laravel collection remove by value. I explained simply about how to remove item by value in laravel collection. you can see laravel collection delete by value. This post will give you a simple example of laravel collection remove item by value.
In this example, we will use reject() method to remove element by value in laravel collection.
The reject method in Laravel collections is used to filter a collection by excluding elements that match a specified condition. It returns a new collection containing all elements that do not meet the specified condition. Here's the basic syntax for using the reject method:
Here's how you can use the reject() method in Laravel:
Example 1:
you can see the below 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(['Hardik', 'Paresh', 'Mahesh', 'Rakesh']);
$valueToRemove = 'Rakesh';
$filteredCollection = $collection->reject(function ($item) use ($valueToRemove) {
return $item === $valueToRemove;
});
dd($filteredCollection->toArray());
}
}
Output:
Array
(
[0] => Hardik
[1] => Paresh
[2] => Mahesh
)
Example 2:
you can see the below 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' => 'Hardik', 'email' => 'hardik@gmail.com'],
['id' => 2, 'name' => 'Paresh', 'email' => 'paresh@gmail.com'],
['id' => 3, 'name' => 'Rakesh', 'email' => 'rakesh@gmail.com'],
]);
$valueToRemove = 'Paresh';
$filteredCollection = $collection->reject(function ($item) use ($valueToRemove) {
return $item['name'] === $valueToRemove;
});
dd($filteredCollection->toArray());
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[email] => hardik@gmail.com
)
[2] => Array
(
[id] => 3
[name] => Rakesh
[email] => rakesh@gmail.com
)
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Collection Remove Last Item Example
- Laravel Collection Remove First Item Example
- How to Get Random Item from Laravel Collection?
- How to Convert Collection to Array in Laravel?
- Laravel Convert Collection to Array Example
- Laravel Collection Get Unique Values Example
- Laravel Collection Get First and Last Item Example
- Laravel Collection intersect() and intersectByKeys() Method Example
- Laravel Collection Implode Method Example
- Laravel Collection Has Method Example
- Laravel Collection Flip Method Example
- Laravel Collection Map Method Example
- Laravel Collection Except() Method Example