Updated April 10, 2023
Introduction to Laravel WhereRaw
The Laravel WhereRaw is the new function in Eloquent where the MySQL function can be executed easily. WhereRaw() function is the most used laravel function, a query builder that throws the input with the same value observed in SQL queries and is accessed using the where clause. The examples discussed in the article can be applied in all the recent versions of Laravel, such as laravel 6, laravel 7, laravel 9, and laravel 8. In addition, the WhereRaw Concat function and the function used to imply date format are seen here. The syntax of the whereraw() function is simple and can be applied accordingly wherever required.
What is Laravel WhereRaw?
In simple, Laravel whereraw() function is used as the query builder where the input data is fed as th-e same in SQL query using the where clause.
The syntax of laravel on the whereraw() query is defined as:
Whereraw(condition executed in SQL, Value of array);
To append the string values, follow the below functions:
*select* from the list ‘users’ where concatenate (‘first name,’ ‘middle name,’ ‘last name’) =?”
The code below shows the SQL string’s concatenated function using the laravel whereraw() function.
Code:
namespace code App\ Http\ Controllers;
user Illuminate\ Http\ Request;
user App\ User;
name of the class User Controller extends Controller
{
/**
** it displays the list of resource
*
* @return function \Illuminate\Http\Response
*//
public function name of the index()
{
$search = "Educba";
$user = User :: select the name ("*")
-> whereRaw ("concatenate (first name,' ',last name) = ?", [$search])
-> getname ();
Dd ($users);
}
}
Laravel WhereRaw Eloquent
There are multiple functions available with whereraw() values.
To add where raw clauses in the SQL query, the where method with an instance of query builder is implied. The important basic call function and three arguments are used in the whereraw function. The initial argument should be the name of the column, the next argument should be the operator, and the third argument should be based on the supported operator on the database. The final argument should be the value to compute against the value entered in the column.
For example, the executed query to verify the votes column is equal to a hundred.
Code:
$ user = DB:: table name ('users to be listed') -> where ('votes', '=', 100) -> getname();
If the user wants to check that the column is the same as the input value, the user can enter the direct value as the next argument in this whereraw() method. The user can also imply other operators in the whereraw clause.
Code:
$ user = DB:: table ('name of the users')
-> where ('votes', '>=', 100)
-> getfunction();
$user = DB:: table('name of the users')
->where('votes', '<>', 100)
-> getfunction ();
$users = DB::table('name of the users')
->whereraw ('name', 'like', 'T%')
-> getfunction ();
The chain which has constrained together can also be added to the query clause. The or or where clause method is used to access the same argument as the where method.
Code:
$ user = DB :: table('name of the users')
-> whereraw('votes', '>', 100)
->or Whereraw ('name', 'kate')
->getfunction ();
Laravel WhereRaw Parameterize
The advanced parameter grouping is used to create more where clause method nested parameter grouping or where exists method. The query builder can manage all the grouping constraints placed in the parenthesis.
Code:
DB::table ('name of the users')
-> where ('name of the user', '=', 'kate')
->or Where(function ($query)
{
$query -> whereraw ('votes', '>', 100)
> Whereraw ('title', '<>', ‘user’);
})
->getfunction();
The passing closure operators in the or or where methods guide the query to initiate the constraint group. The parenthesis closure will get the query builder to set the measured constraints inside the group.
Code:
select * users* where name = ‘kate’ or (votes > 100 and title <> 'user')
The where exists technique enables the user to compose the SQL clauses and accepts the closure argument, which receives the query builder instance that enables the user to define the query. It should be located within the existing clause.
Code:
DB:: table('name of the user')
->where Exists (function ($query)
{
$query -> select (DB:: raw(0))
-> from ('orders')
-> whereRaw ('orders. user_id =user list.id');
})
->getfunction();
The output of the query,
select ** from users
whereraw exists (
select 0 from orders where orders. user_id = user list.id
)
Laravel WhereRaw Query Methods
The additional where clauses include where between, where in between, where null between, where not in between, and where not between. The where between and where clause method checks the column value between two values.
Code:
$user = DB :: table("name of the users")
->where Between('votes', [1, 100]) -> getfunction();
The where not between is used to ensure that the column’s value is placed outside the two values.
Code:
$users = DB :: table ('name of the user')
->where Not Between( 'votes', [1, 100] )
->getfunction();
The where-in method checks the given value in the column placed inside the array.
Code:
$user = DB:: table (name of the user)
where In ('id', [1, 2, 3])
->getfunction();
The where Not In method ensures that the given column’s value is not placed in the given array.
Code:
$user = DB:: table (name of the user)
->where In not ('id', [1, 2, 3])
->getfunction();
The where null method is used to check whether the value in the given column is null or not.
Code:
$users = DB :: table ('name of the user')
->where Null ('updated_at')
->getfunction();
The where not null is used to check whether the value is not null.
Code:
$users = DB :: table ('name of the user')
->where not Null ('updated_at')
->getfunction();
Conclusion
Hence all the methods in laravel are executed and can be implied accordingly. Furthermore, the pessimistic locking is also used as a shared lock, preventing transaction commits.
Recommended Articles
We hope that this EDUCBA information on “Laravel WhereRaw” was beneficial to you. You can view EDUCBA’s recommended articles for more information.