What is meant by whereRaw in PHP Laravel framework

Asked
Active3 hr before
Viewed126 times

10 Answers

laravelmeant
90%

WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause. ,Think of it as the where() function whose input argument will not be processed before inserting into queries. ,Now Laravel put this where clause as it is in your query, like below: ,WhereRaw: Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings.

See the example below:

$Query = DB::table('some_table') - > where('YEAR(date)', 'YEAR(CURRENT_DATE)');

In this Laravel will resolve your arguments to build a query. Which will result in the following query because your input will be treated as some field and its its value :

SELECT * FROM `some_table`
WHERE `YEAR(date)` = `YEAR(CURRENT_DATE)`

And now if you use whereRaw like:

$Query = DB::table('some_table') - > whereRaw('YEAR(date) = YEAR(CURRENT_DATE)');

Now Laravel put this where clause as it is in your query, like below:

SELECT * FROM `some_table`
WHERE YEAR(date) = YEAR(CURRENT_DATE)
load more v
88%

Sometimes you may need to insert an arbitrary string into a query. To create a raw string expression, you may use the raw method provided by the DB facade:, Database ,Running Database Queries Chunking Results Streaming Results Lazily Aggregates ,You may use whereJsonLength method to query JSON arrays by their length:

You may use the table method provided by the DB facade to begin a query. The table method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally retrieve the results of the query using the get method:

< ? php

namespace App\ Http\ Controllers;

use App\ Http\ Controllers\ Controller;
use Illuminate\ Support\ Facades\ DB;

class UserController extends Controller {
   /**
    * Show a list of all of the application's users.
    *
    * @return \Illuminate\Http\Response
    */
   public
   function index() {
      $users = DB::table('users') - > get();

      return view('user.index', ['users' => $users]);
   }
}
load more v
72%

Laravel provide new eloquent function whereRaw(), using whereRaw you can easily use mysql function and make it easily where condition. so here i will give you two example one concat function and another date_format function with whereRaw.,Now, let's see post of laravel whereraw example. this example will help you laravel eloquent whereraw example. you will learn how to use whereraw in laravel. let’s discuss about whereraw concat in laravel 6, laravel 7 and laravel 8 project.,Laravel Eloquent Left Join Where Null Condition Example,Laravel Collection Merge | How to Merge Two Eloquent Collection?

whereRaw(SQL Condition, Array Value);
load more v
65%

Search Answer Titles

$camps = $field - > camps() - > where('status', 0) - > where(function($q) {
   $q - > where('sex', Auth::user() - > sex) - > orWhere('sex', 0);
}) - > get();
75%

Laravel Blade Isset Example,Laravel WhereBetween Query Example,Laravel WhereNotNull Query Example ,Laravel 7/6 Where Like Query Example

"select * from `users` where YEAR(created_at) = ?"
load more v
40%

Laravel has a great database mechanism called Eloquent, also a powerful Query Builder, but sometimes it makes sense to just use plain SQL, in the form of Raw Queries. In this article, I will show you the most common examples of this approach.,A pretty typical example is when you have an SQL statement from some older project, and you need to convert it to Eloquent or Query Builder.,In my experience, it’s often used in database migrations, when some table structure changes and old data needs to be updated with a new structure. ,If you need to execute some SQL query, without processing any results, like INSERT or UPDATE without any parameters, you can use DB::statement().

Example from Laravel documentation:

$users = DB::table('users') -
   > selectRaw('count(*) as user_count, status') -
   > where('status', '<>', 1) -
   > groupBy('status') -
   > get();
load more v
22%

To demonstrate SQL injection in the whereRaw RawMethod, let’s take a look at the following code: ,Before we dive into examples of Laravel SQL injection, let’s take a look at what SQL injection is. ,This will cause the application to return zero rows or crash. This behavior shows the existence of SQL injection vulnerability in the whereRaw part of our initial query. ,The value for id is defined by user input. Let’s look at what happens when a user enters the following value: 

SELECT * FROM users WHERE username = 'john.doe'
load more v
60%

Start Your Free Software Development Course,Software Development Course - All in One Bundle,Validation in Laravel,All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

select * from posts;

select * from posts;
load more v
48%

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent) , A MongoDB based Eloquent model and Query builder for Laravel (Moloquent) ,You can use MongoDB either as the main database, either as a side database. To do so, add a new mongodb connection to config/database.php:,This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.

[3.8] Fix getRelationQuery breaking changes
load more v
23%

If you need to use sql aggregate function with where clause in laravel query builder then you can do it using db raw or whereraw function of laravel eloquent. here you will learn how to use db raw with where clause., Laravel ,In this small tutorial i will give two way to use sql aggregate function with where clause. there are two way : 1) DB::raw() and 2) whereRaw(). So here basically i will give you very simple example on both way, you can understand and use it well in your laravel 5 application., How to use Livewire in Laravel?

$users = \DB::table("users") - > where(DB::raw("(DATE_FORMAT(created_at,'%d-%m-%Y'))"), '19-01-2018') - > get();
dd($users);
load more v

Other "laravel-meant" queries related to "What is meant by whereRaw in PHP Laravel framework"