Laravel Collection diff(), diffAssoc() and diffKeys() Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

This tutorial shows you laravel collection diff lesson. you will learn laravel collection difference. This article will give you simple lesson of laravel collection diff assoc lesson. We will look at lesson of laravel collection diff keys lesson. Alright, let’s dive into the steps.

I will give you simple lessons of diff, diffAssoc and diffKeys colletion in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7 and laravel 8 application. so let's see bellow lesson that will helps you lot.

Laravel Collection diff() Example

Syntax:

$collecton->diff(

Array OR Collection

);

Example

public function index()

{

$collectionA = collect([1, 2, 3, 4, 5, 6]);

$collectionB = collect([3, 4, 6]);

$collection = $collectionA->diff($collectionB);

dd($collection);

}

Result:-

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => 1

[1] => 2

[4] => 5

)

)

Laravel Collection diffAssoc() Example

Syntax:

$collecton->diffAssoc(

Array OR Collection

);

Example

public function index()

{

$collectionA = collect([

'id' => 1,

'name' => 'Sagor',

'type' => 'Admin'

]);

$collectionB = collect([

'id' => 2,

'name' => 'Paresh',

'type' => 'Admin'

]);

$collection = $collectionA->diffAssoc($collectionB);

dd($collection);

}

Result:-

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[id] => 1

[name] => Sagor

)

)

Laravel Collection diffKeys() Example

Syntax:

$collecton->diffKeys(

Array OR Collection

);

Example

public function index()

{

$collectionA = collect([

'id' => 1,

'name' => 'Sagor',

'type' => 'Admin',

'city' => 'Mumbai'

]);

$collectionB = collect([

'id' => 2,

'name' => 'Paresh',

'type' => 'Admin'

]);

$collection = $collectionA->diffKeys($collectionB);

dd($collection);

}

Result:-

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[city] => Mumbai

)

)

thank you very much for following up with me.

Recent Posts

Related Posts