laravel illuminate database eloquent collection object code example

Example 1: Method Illuminate\Database\Eloquent\Collection

use App\Models\User;

$users = $users->intersect(User::whereIn('id', [1, 2, 3])->get());

Example 2: Method Illuminate\Database\Eloquent\Collection

$users = $users->except([1, 2, 3]);

Example 3: Method Illuminate\Database\Eloquent\Collection

$users = $users->fresh();

$users = $users->fresh('comments');

Example 4: Method Illuminate\Database\Eloquent\Collection

$users = App\Models\User::all();

$names = $users->reject(function ($user) {
    return $user->active === false;
})
->map(function ($user) {
    return $user->name;
});

Example 5: laravel collection namespace

Illuminate\Database\Eloquent\Collection

Tags:

Php Example