laravel 5.4 New Feature - Add eloquent whereKey method

By Hardik Savani | January 25, 2017 | Category : Laravel

Yesterday release Laravel 5.4 new version with new features and many upgrade. Laravel also provide documentation for Laravel 5.4 on their website. There are several update in Laravel 5.4 like in collections, mail, factory helper, Bootstrappers etc.

In this post we are going to see whereKey method in Laravel Eloquent. whereKey is very simple way to use and pretty interesting feature. laravel provide us where condition with add key(column) name and then value, But in this method you have write Key = name of column.

So i am going to give one example for whereKey() that way you can understand how it is working. So first we have on "posts" table with some dummy data like as bellow screen shot.

posts table

Ok, now we require to get only status = 'PUBLISHED' records. So now using whereKey() through we can make query as like bellow:

whereKey() Example

Route::get('wherekey', function () {

$posts = \DB::table('posts')






You will be found out put as like bellow:


Illuminate\Support\Collection Object


[items:protected] => Array


[0] => stdClass Object


[id] => 21

[title] => My Sample Post

[status] => PUBLISHED


[1] => stdClass Object


[id] => 23

[title] => Latest Post

[status] => PUBLISHED


[2] => stdClass Object


[id] => 27

[title] => Yarr Post

[status] => PUBLISHED




I hope it can help you...

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: