ItSolutionStuff.com

Laravel Where Condition with Two Columns Example

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel withCount() with Where Condition Example

Read Now →

Laravel Where Clause with Function Query Example

Read Now →

Laravel Sum Query with Where Condition Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Eloquent Left Join Where Null Condition Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Eloquent whereNull() Query Example

Read Now →

How to use whereHas with orWhereHas in Laravel?

Read Now →

Laravel Relationship Where Condition Example

Read Now →

Laravel Where Clause with date_format() Example

Read Now →

How to Use Multiple Database in Laravel?

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

Laravel Join with Subquery in Query Builder Example

Read Now →