Laravel Where Clause with Function Query Example 2022

Hello dev, Today we are going to learn how to use where clause with function query with example. This tutorial cover on laravel WHERE CLAUSE with function query with example.

This article goes in detailed on laravel where function query. It’s a simple example of how to use function with where clause laravel.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

Laravel eloquent provides where clause query. But if you want to use multiple where conditions with or where then how you will use with laravel eloquent. Then laravel provides the where condition with a function query. So Here, I will give you a simple example of how to use a function with where clause in laravel eloquent. let’s see a simple example.

Also Read: [ New ] Laravel Google 2FA Authentication Tutorial Example (2022)

Examples for Laravel Where Clause with Function Query

Example 1: Laravel where() Clause with Function Query

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select("*")
                        ->where("is_published", 1)
                        ->where(function($query) {
                                $query->where('category', 'Laravel')
                                      ->orWhere('viewer', '>', 100);
                            })
                        ->get();    
        dd($posts);
    }
}

Query:

SELECT * FROM posts WHERE is_published == 1 AND (category = 'Laravel' OR viewer > 50)

Also Read: How to Use Limit and Offset in Laravel Eloquent? (2022)

Example 2: Laravel orWhere() Clause with Function Query

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */

    public function index(Request $request)
    {
        $posts = Post::select("*")
                        ->where("viewer", ">", 100)
                        ->orWhere(function($query) {
                                $query->where('category', 'Laravel')
                                      ->where('viewer', '>', 50);
                            })
                        ->get();
        dd($posts);
    }
}

Query:

SELECT * FROM posts WHERE viewer > 100 OR (category = 'Laravel' AND viewer > 50)

Also Read: How to Find Multiple Ids using Laravel Eloquent? (2022)

Related Question also Ask

  • How to pass multiple values in where condition in Laravel?
  • How do I use whereBetween in Laravel?
  • What is query () in Laravel?
  • Where IS NOT NULL eloquent?
  • What is DB :: Raw in Laravel?
  • What is ORM in Laravel?

Conclusion

Today, We had learn Laravel Where Clause with Function Query Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel Order By Multiple Columns Example (2022)

2 Comments

Leave a Reply