in laravel querey builder easily compare two and more database columns by using whereColumn method and we also get only rows data which column values are same.
Laravel whereColumn() method
in example we get courses table rows that start date and created date are same.
$courses = DB::table('courses') ->whereColumn('created_at', 'start_date') ->get();
we also calculate two cloumn according compare grater than and less than values. In given example only rows gate which start date is grater than to create date.
$courses = DB::table('courses') ->whereColumn('created_at','<', 'start_date') ->get();
an another example of whereColumn two compare more than two columns.let get all data which column1 is grater than price column2 and column1 is less than column3. Notice that all three columns have integer values. is also call that compare an array columns in whereColumn() method.
$courses = DB::table('courses') ->whereColumn([ ['column1', '>', 'column2'], ['column1', '<', 'column3'] ]) ->get();
watch all query builder playlist click on sidebar playlist link
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- laravel query builder introduction tutorial
- part 1 laravel query insert data
- part 2 laravel all type of where conditions in query builder
- part 3 laravel whereDate, whereYear and whereTime qyery method
- part 4 compare two db columns in laravel query builder
- part 6 laravel pluck method get single column value
- part 7 laravel select and addselect query in query builder
- part 5 laravel first and find queries
- part 8 laravel edit and update method query explaination
- Part 9 laravel query for update or insert data by one method
- part 10 laravel delete vs truncate vs drop method
- part 11 gropupby and orderby query tutorial
- Part 12 laravel union of two queries
- part 13 laravel all aggregate functions with example
- part 14 laravel query builder chunk method
- part 15 laravel exists() and doesntExist() method
- part 16 laravel paginate vs simplePaginate method
- part 17 laravel all join methods