Inside this article we will see the concept i.e Laravel 9 Where Clause with Function Query Example Tutorial. Article contains the classified information about How to use function with where clause laravel.
If you want to use laravel eloquent where function query then there are several ways to do it. We can do query grouping of OR and AND mysql operator.
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.
Read More: Laravel 9 Eloquent How To Find Data Using Multiple Ids Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Method #1: Laravel where() Clause with Function Query
We will use laravel eloquent methods to create query grouping of AND and OR.
<?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); } }
Read More: Laravel 9 Partially Hide or Mask Email Addresses Tutorial
Generated Query
SELECT * FROM posts
WHERE is_published = 1 AND (category = 'Laravel' OR viewer > 100)
Method #2: Laravel orWhere() Clause with Function Query
We will use laravel eloquent methods to create query grouping of OR and AND.
<?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("is_published", 1); }) ->get(); dd($posts); } }
Generated Query
SELECT * FROM posts
WHERE viewer > 100 OR (category = 'Laravel' AND is_published = 1)
We hope this article helped you to learn about Laravel 9 Where Clause with Function Query Example Tutorial in a very detailed way.
Read More: CodeIgniter 4 Partially Hide or Mask Email Addresses Tutorial
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.