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 DataTable Solution with Tailwind & Bootstrap Support

Laravel DataTable Solution with Tailwind & Bootstrap Support

Learn how to replace heavy jQuery DataTables with the lightweight, framework-agnostic vanillajs-datatable package—perfect for Laravel, Blade, and Tailwind CSS.

1 month ago Read article →
Building RESTful APIs in Laravel 11: A Simple Guide

Building RESTful APIs in Laravel 11: A Simple Guide

Building RESTful APIs in Laravel 11: A Simple Guide

4 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

4 months ago Read article →
Multi language in laravel 11

Multi language in laravel 11

Multi language in laravel 11

4 months ago Read article →