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(); ✔
Tags
Wherekey Laravel Laravel Query Method Laravel Where Clause Wherekey Method