Laravel Merge Unique values in the Collection
Teams applying “Laravel Merge Unique values in the Collection” in a production project can use risk adjusted return capital to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hello Friends,
In this quick example, let's see laravel merge unique values in the collection. Here you will learn how to merge the unique values of a collection in laravel. I’m going to show you the laravel collection merge unique values example. it's a simple example of laravel merging unique values into the collection. So, let's follow a few steps to create an example of a laravel collection that merges unique values.
You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.
You have just to follow the below step and you will get the layout as below:
Step 1: Install Laravel
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create Format Route
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MergeUniqueController;
/*
|--------------------------------------------------------------------------
| 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('/index',[MergeUniqueController::class, 'index']);
Step 3: create MergeUniqueController we will create MergeUniqueController. so you can see the below code with output:
php artisan make:controller MergeUniqueController
App\Http\Controllers\MergeUniqueController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RemoveFirstController extends Controller
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
$firstCollection = collect(['One', 'Two', 'Three']);
$secondCollection = collect(['Three', 'Four', 'Five']);
$mergedCollection = $firstCollection->merge($secondCollection);
$mergedCollection = $mergedCollection->unique(function ($item) {
return $item;
});
$mergedCollection->all();
dd($mergedCollection);
}
}
Step 4: Start Development Server
Start the development server. Use the PHP artisan serve command and start your server:
php artisan serve
Now you are ready to run our example so run the below command to quick run.
http://localhost:8000/index
Output:
#items: array:5 [▼
0 => "One"
1 => "Two"
2 => "Three"
4 => "Four"
5 => "Five"
]
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial