Laravel whereColumn Eloquent Query Example

Laravel where column Query Example; In this tutorial you will learn how to use eloquent wherecolumn query in laravel application. Many times we need to get one or more column s so we can use easy and simply ways to wherecolumn in laravel. The whereColumn method may be used to verify that two columns are equal.

Let’s see the below examples to use whereColumn eloquent query in laravel. You can implement whereColumn query in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 version.

Example 1:

The whereColumn method may be used to verify that two columns are equal:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $users = User::whereColumn('first_name','last_name')
                    ->get();
        dd($users);
    }
}

Example 2:

You may also pass a comparison operator to the whereColumn method:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $users = User::whereColumn('updated_at', '>', 'created_at')
                    ->get();
        dd($users);
    }
}

Example 3:

You may also pass an array of column comparisons to the whereColumn method. These conditions will be joined using the and operator:

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $users = DB::table('users')
                    ->whereColumn([
                        ['first_name', '=', 'last_name'],
                        ['updated_at', '>', 'created_at'],
                    ])->get();
        dd($users);
    }
}

Hope these examples work for you..

Leave a Comment