How to use whereColumn() method in Laravel

Categorized as Laravel Tagged ,

Quick tip: you can use a pretty handy Eloquent method called whereColumn() to verify if columns are equal. For example, you can find posts that have been updated or posts which have the same title and slug. Here is the code:

// Posts that have been updated
Post::query()
  ->whereColumn('updated_at', '>', 'created_at')
  ->get();


// Posts with identical title and slug
Post::query()
  ->whereColumn('title', 'slug')
  ->get();

Read more in the official documentation here.

Leave a reply

Your email address will not be published.