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

How to Add Two-Factor Authentication (2FA) in Laravel Fortify

How to Add Two-Factor Authentication (2FA) in Laravel Fortify

How to Add Two-Factor Authentication (2FA) in Laravel Fortify

2 months ago Read article →
Using spatie/browsershot in Laravel for Screenshot and PDF Generation

Using spatie/browsershot in Laravel for Screenshot and PDF Generation

Learn how to use the Spatie Browsershot package in Laravel to capture screenshots and generate PDFs easily. This guide walks you through the setup process and shows you how to create high-quality images and documents from your web pages.

2 months ago Read article →
Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

2 months ago Read article →
How to Implement Custom Facebook OAuth Login in Laravel  Without Socialite

How to Implement Custom Facebook OAuth Login in Laravel Without Socialite

How to Implement Custom Facebook OAuth Login in Laravel Without Socialite

2 months ago Read article →