The database query builder in Laravel provides a simple, intuitive interface for generating and performing database queries. It works with every one of Laravel's supported database systems and may be used to conduct most operations on the database in your application.

We'll look at the whereKey method in Laravel Eloquent in this post. whereKey is an extremely easy-to-use tool with a lot of potential. Where condition with add key(column) name and then value is provided by Laravel. However, in this manner, you must write Key = column name.

So, in order for you to understand how whereKey() works, I'll give you an example. So, first, we'll create a table called "posts" with some dummy data.

whereKey() Example

Route::get('wherekey', function () {
$posts = \DB::table('posts')
->select(["id","title","status"])
->whereStatus("PUBLISHED")
->get();
dd($posts);
});

You will find output similar to what is seen below.

Output:
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 you will like the content and it will help you to learn Laravel Eloquent Query - Laravel whereKey method
If you like this content, do share.


Recommended Posts

View All

Laravel 9 Upload image using Summernote editor


A basic Javascript package called Summernote Editor can be used to create online WYSIWYG web editors.

How to Install and Use MomentJS in Laravel 9 Application


Learn how to easily install and use MomentJS in your Laravel 9 application with our step-by-step guide. Improve your date and time management today!

Laravel 8 Custom 404, 500 Error Page Example


How to create custom error page in Laravel 8 and we will also try to tell you why we required to create the custom error page.

Laravel 9 Capture Image from Webcam and Store in Database


Learn how to capture images from webcam using Laravel 9 and store them in a database. Step-by-step guide with code examples and best practices.

Unlocking the Power of Data Visualizing Laravel App Data with Chartello


Discover the potential of data visualization with Chartello, a Laravel app that helps you unlock insights from your data. Try it now and see the resul...