Laravel whereRaw havingRaw and orderByRaw Example

Today, We want to share with you Laravel whereRaw havingRaw and orderByRaw Example.In this post we will show you How To Select Raw SQL Using Eloquent, hear for laravel selectraw bindings we will give you demo and example for implement.In this post, we will learn about Laravel Query Builder, selectRaw or select and raw with an example.

Laravel whereRaw havingRaw and orderByRaw Example

There are the Following The simple About Laravel whereRaw havingRaw and orderByRaw Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Display Raw SQL query from Query Builder in Laravel, so the some major files and Directory structures for this example is following below.

Laravel whereRaw query methods

whereRaw

$products = DB::table('products')
    ->whereRaw('price > IF(state = "TX", ?, 100)', [200])
    ->get();

Laravel havingRaw query methods

havingRaw

Product::groupBy('product_id')->havingRaw('COUNT(*) > 1')->get();

Laravel orderByRaw query methods

orderByRaw

laravel orderByRaw() on the query builder

User::where('created_at', '>', '2022-01-01')
  ->orderByRaw('(updated_at - created_at) desc')
  ->get();

$product = "
    match (
        `name`,
        `information`
    ) against (
        ?
        IN BOOLEAN MODE
    )";

$category = 'mobile';

$sql->whereRaw($product, array($category));
$sql->orderByRaw($product . " DESC", array($category));

Laravel “Where” Eloquent Examples

->where('product_desc','value')
->where('product_desc','LIKE','%'.$value.'%')
->where(function ($query) {
  $query->where('a', '=', 1)
      ->orWhere('b', '=', 1);
})
->orWhere('product_desc','!=', 'value')
->whereRaw('age > ? and repututaions = 100', array(25))

->whereRaw(DB::raw("id in (select area_code from bios GROUP BY bios.area_code)"))

->whereExists(function($query){
  $query->select(DB::raw(1))
      ->from('users')
      ->whereRaw('users.mang_id = managements.id')
      ->groupBy('users.mang_id')
      ->havingRaw("COUNT(*) > 0");
})
->orWhereExists()
->whereNotExists()
->orWhereNotExists()

->whereIn('product_desc',[10,25,26])
->orWhereIn()
->whereNotIn('id', function($query){
  $query->select('area_code')
  ->from('bios')
  ->groupBy('bios.area_code');
})
->whereNotIn()
->orWhereNotIn

->whereNull('product_desc')
->orWhereNull('product_desc')
->whereNotNull('product_desc')
->orWhereNotNull('product_desc')

->whereDay()
->whereMonth('product_desc', '=', 1)
->whereYear('product_desc', '>', 2020)
->whereDate('product_desc', '>', '2025-01-01')

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about sql to laravel query builder online.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   store all record using laravel Cache Example
  2.   Debugging Eloquent Queries in Laravel
  3.   Laravel Eloquent Update increment decrement Database column value
  4.   PHP Codeigniter Insert Multiple Records Into MySQL
  5.   PHP Generate PDF from MySQL Database using FPDF
  6.   PHP Laravel Remove Brackets Example
  7.   How to create model object in controller in laravel?
  8.   Laravel 5.8 Merge Multiple Collection Paginate
  9.   Laravel All Where eloquent methods with Examples
  10.   jQuery Ajax Secure Login Registration System in PHP and MySQL