Laravel 10|9|8 Multiple Where Condition Example

Sometimes, you may want to use fetch/get, insert, update, and delete data with multiple where conditions in laravel apps, This tutorial will show you various examples of how to use multiple where conditions with eloquent queries.

And also you can use the where clause with Laravel search query with multiple conditions, update query with multiple conditions, delete query with multiple conditions, and relationship with multiple conditions in laravel.

So, In this tutorial, you will learn how to use where with multiple conditions in query using laravel eloquent.

Multiple where Condition in Laravel 10|9|8

Here are some examples of multiple where cluase conditions in query with laravel:

  • Example 1: Multiple where Clause Condition in Laravel Query Builder
  • Example 2: Multiple WHERE clauses using DB::table query
  • Example 3: Laravel multiple where with Scope
  • Example 4: Laravel eloquent delete record using multiple where condition without id?
  • Example 5: Insert with Multiple Where Conditions
  • Example 6: Update with Multiple Where Conditions

Example 1: Multiple where Clause Condition in Laravel Query Builder

Let’s use the following example for multiple where clause condition in laravel query builder:

public function index()
{
    $users = User::where('status', 1)
                ->where('is_banned', 0)
                ->get(['name']);

    dd($users);
}

Example 2: Multiple WHERE clauses using DB::table query

Example of multiple where clause using db::table() query in laravel:

public function index()
{
    $users = DB::table('users')->where([["status" => 1], ["is_banned" => 0]])
                ->get(['name']);

    dd($users);
}

Example 3: Laravel multiple where with Scope

Here is an example of multiple where condition using scope in laravel:

public function index()
{
    //Then call the scopes as given below
    $users = User::active()->that()->get();
}

Example 4: Laravel eloquent delete record using multiple where condition without id?

In Laravel, you can use the Eloquent ORM (Object-Relational Mapping) to perform database operations like deleting records with multiple where conditions. Here’s an example of how you can delete records using multiple where conditions:

Suppose you have an Eloquent model named Product and you want to delete products that match certain conditions, such as a specific category and a minimum price. Here’s how you would do it:

use App\Models\Product; // Make sure to import your Product model
// ...
$category = 'Electronics';
$minPrice = 100;
$deletedRows = Product::where('category', $category)
    ->where('price', '>=', $minPrice)
    ->delete();
if ($deletedRows > 0) {
    return "Successfully deleted $deletedRows products.";
} else {
    return "No products matched the delete criteria.";
}

Example 5: Insert with Multiple Where Conditions

In this example, you create a new Product instance, set its attributes, and then call the save() method to insert the record into the database.

use App\Models\Product;
// ...
$categoryId = 1;
$productName = 'New Product';
$productPrice = 50;
$product = new Product();
$product->category_id = $categoryId;
$product->name = $productName;
$product->price = $productPrice;
$success = $product->save();
if ($success) {
    return "Product inserted successfully.";
} else {
    return "Failed to insert product.";
}

Example 6: Update with Multiple Where Conditions

Here, you use the update() method on the query builder. The where conditions determine which records should be updated, and the update() method changes the price attribute of those records.

use App\Models\Product;
// ...
$categoryId = 2;
$productName = 'Updated Product';
$newPrice = 60;
$affectedRows = Product::where('category_id', $categoryId)
    ->where('name', $productName)
    ->update(['price' => $newPrice]);
if ($affectedRows > 0) {
    return "Updated $affectedRows product(s).";
} else {
    return "No products were updated.";
}

Conclusion

To create multiple where clause query using laravel eloquent, you have learned how to use multiple where conditions with the query builder and eloquent model in laravel apps.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Laravel 10|9|8 Multiple Where Condition Example

Leave a Reply

Your email address will not be published. Required fields are marked *