duplicates laravel code example

Example 1: collection laravel filter

$collection = collect([1, 2, 3, 4]);

$filtered = $collection->filter(function ($value, $key) {
    return $value > 2;
});

$filtered->all();

// [3, 4]

Example 2: laravel eloquent duplicate record

// Retrieve the first task
$task = Task::first();

$newTask = $task->replicate();
$newTask->project_id = 16; // the new project_id
$newTask->save();

Example 3: laravel collection find duplicates

$users = Users::all();
$usersUnique = $users->unique('id');
$usersDupes = $users->diff($usersUnique);

dd($users, $usersUnique, $usersDupes);

Example 4: laravel pluck

$users = Users::pluck('name','email');
dd($users);

Tags:

Php Example