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

Multi-Guard Authentication with Laravel Fortify

Multi-Guard Authentication with Laravel Fortify

Multi-Guard Authentication with Laravel Fortify

1 month ago Read article →
Better Error Handling with onFailure Callback in Laravel's DB::transaction() (New in Laravel 12.9)

Better Error Handling with onFailure Callback in Laravel's DB::transaction() (New in Laravel 12.9)

Laravel 12.9 now supports onFailureCallback in DB::transaction(), making database error handling easier with automatic rollback and failure notifications.

1 month ago Read article →
Laravel 11.30 Introduces HasUniqueStringIds for Simple Unique IDs

Laravel 11.30 Introduces HasUniqueStringIds for Simple Unique IDs

Laravel 11.30 Introduces HasUniqueStringIds for Simple Unique IDs

1 month ago Read article →
Install tailwindcss in Laravel 11

Install tailwindcss in Laravel 11

Install tailwindcss in Laravel 11

1 month ago Read article →