Laravel Where Condition with Two Columns Example

By Hardik Savani April 16, 2024 Category : Laravel

Hey Folks,

This detailed guide will cover laravel where with two column example. We will use two column where laravel. I would like to show you laravel whereColumn. I explained simply about table column condition in laravel.

Laravel introduce whereColumn() in Query Builder, that way we can compare two column like simple where condition. We sometimes require to check this type of condition.

you can use wherecolumn() eloquent function in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

In this example code, i have simple "items" table and i want to get only created_at and updated_at column should equals. So you can also check bellow laravel eloquent as bellow:

Example 1:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$data = Item::select("*")

->whereColumn('created_at','updated_at')

->get();

dd($data);

}

}

Example 2:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$data = Item::select("*")

->whereColumn('created_at','>','updated_at')

->get();

dd($data);

}

}

I hope it can help you....

Tags :
Shares