Your Guide to Laravel Excellence

Laravel Tip: The "whereKey" Method

Laravel Tip: The "whereKey" Method

Instead of WhereIn you can use WhereKey .It makes statements more readable and you don't need to remember the name of the primary key.

In Laravel, the whereKey method is used to filter a query based on the primary key values of the model.

You can pass an array of primary key values to the whereKey method, and it will fetch the corresponding records.

Post::whereIn('id', [1,2,3,4,5,6,7])->get();
Post::whereNotIn('id', [1,2,3,4,5,6,7])->get();

Post::whereKey([1,2,3,4,5,6,7])->get();
Post::whereKeyNot([1,2,3,4,5,6,7])->get();

Recommeded Posts

Laravel Tailwind DataTable with Vanilla JS (No jQuery)

Laravel Tailwind DataTable with Vanilla JS (No jQuery)

Build fast, responsive Laravel DataTables with Tailwind CSS v4 and vanillajs-datatable. No jQuery, no frameworks. Just pure JS + Blade.

2 weeks ago Read article →
Upload Huge File - Mastering Chunked File Uploads in Laravel

Upload Huge File - Mastering Chunked File Uploads in Laravel

Upload Huge File - Mastering Chunked File Uploads in Laravel

3 months ago Read article →
Learn Laravel Scopes and simplify Eloquent Queries

Learn Laravel Scopes and simplify Eloquent Queries

Learn Laravel Scopes and simplify Eloquent Queries

3 months ago Read article →
How to Add Real-Time Comments in Laravel 11 with Laravel Reverb

How to Add Real-Time Comments in Laravel 11 with Laravel Reverb

How to Add Real-Time Comments in Laravel 11 with Laravel Reverb

3 months ago Read article →