📜  laravel whereRaw - PHP (1)

📅  最后修改于: 2023-12-03 15:02:35.795000             🧑  作者: Mango

Laravel whereRaw - PHP

Laravel whereRaw is a powerful method that allows you to add custom SQL clauses to your Laravel query builder. This method is very useful when you need to use a SQL function that is not directly supported by Laravel's query builder.

Syntax

The syntax of whereRaw method is as follows:

public function whereRaw($sql, $bindings = [], $boolean = 'and')

Parameters:

  • sql: The raw SQL to add to the query.
  • bindings (optional): The values to bind to the SQL statement.
  • boolean (optional): The operator to use for combining the clause with the previous one.
Example

In this example, we will use the whereRaw method to add a custom SQL statement to our query.

$users = DB::table('users')
            ->whereRaw('id > ? and name = ?', [5, 'John'])
            ->get();

In the example above, we added a custom SQL statement that retrieves all users whose id is greater than 5 and whose name is equal to John.

The whereRaw method takes two parameters. The first parameter is the raw SQL statement and the second parameter is an array of values to bind to the SQL statement.

In the above example, we used ? placeholders in the SQL statement and passed an array of values as the second parameter to the whereRaw method.

Conclusion

In conclusion, the whereRaw method is a powerful tool that allows you to add custom SQL clauses to your Laravel query builder. By using this method, you can add complex SQL queries to your application easily.