How to use the Where() method in Laravel

Overview

The where clause queries a database.

In this shot, we will learn to use the where() query builder to query the database. This is an important and basic concept while writing a query in Laravel.

The where() method

The where() method is a query builder that queries an instance of a data table from a database to get specific row(s) from it.

Syntax

DB::table('users')->where('votes', '>', 100)->get();

Parameters

The where() method accepts basically three parameters;

  1. The column name
  2. The operator(optional)
  3. The query value

There is an existing shot on table() method you can look at it for more understanding.

Explanation

The where() query builder finds a query value match on the database’s column name. It does so to contain or filter down either the result or the return value.

Example

$affected = DB::table('users')
              ->where('id', 1)
              ->update(['votes' => 1]);

From the example above, the database has been updated. We use the where() query builder to locate and update a specific cell on a table’s row. Hence, the row with an id of one on the user’s table has been updated. Specifically, the votes column of that record is updated to 1.

Free Resources