HDTuto.com

How to Remove Item from Collection Laravel?



In this tutorial we will go over the demonstration of remove item from laravel collection. This post will give you simple example of laravel collection remove item. I’m going to show you about laravel collection forget key. In this article, we will implement a laravel collection forget. you will do the following things for how to remove item from collection laravel.

I will give you some examples of how to removing key value pair array to collection in laravel. you can easily add array in laravel 5, laravel 6 and laravel 7.

Let's see example:

Example 1:

public function index()

{

$collection = collect([

'name'=>'Hardik',

'email'=>'hardik@gmail.com'

]);

$collection->forget('name');

$collection->all();

dd($collection);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[email] => hardik@gmail.com

)

)

Example 2:

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik'],

['id'=>2, 'name'=>'Harsukh'],

['id'=>3, 'name'=>'Bhagat'],

]);

$collection->forget(1);

$collection->all();

dd($collection);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

)

[2] => Array

(

[id] => 3

[name] => Bhagat

)

)

)

I hope it can help you...




April 27, 2020 | Category : Laravel



Blog Search