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.