Laravel not executing queries with 'having' clause

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

It appears that queries which contain a 'having' clause are not executed by laravel, instead it pretends to execute them and returns an empty collection. What is going on?,It appears that laravel is preparing the statement, but never executing it. Why not?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,The problems turns out to be that laravel is not correctly resolving DB::raw statements in a having clause. Since it also does not allow arrays to be passed through, this has the practical effect of making it impossible to write a query with having x in (1,2,3). Instead, the solution is to do several separate having clauses. So in this case, like this:

The problems turns out to be that laravel is not correctly resolving DB::raw statements in a having clause. Since it also does not allow arrays to be passed through, this has the practical effect of making it impossible to write a query with having x in (1,2,3). Instead, the solution is to do several separate having clauses. So in this case, like this:

foreach($author_ids as $id) {
   $query - > having('book_author_author_id', '!=', $id);
}
load more v
88%

The orderByRaw method may be used to provide a raw string as the value of the "order by" clause:,The groupByRaw method may be used to provide a raw string as the value of the group by clause:,The havingRaw and orHavingRaw methods may be used to provide a raw string as the value of the "having" clause. These methods accept an optional array of bindings as their second argument:,The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean or sanitize strings passed to the query builder as query bindings.

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%

$users = DB::table('users') -
   > whereIn('id', [1, 2, 3]) -
   > get();
load more v
65%

If you need to execute some SQL query, without processing any results, like INSERT or UPDATE without any parameters, you can use DB::statement().,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.,Guess what, you don’t have to. DB::select() is a perfectly fine statement.,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.

Example from Laravel documentation:

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

Query objects are lazily evaluated. This means a query is not executed until one of the following things occur:,You can execute a query directly without having to use foreach on it. The easiest way is to either call the all() or toList() methods:,After counting, the query can still be used for fetching the associated records:,When using the expression objects you can use the following methods to create conditions:

use Cake\ ORM\ TableRegistry;

// Prior to 3.6 use TableRegistry::get('Articles')
$articles = TableRegistry::getTableLocator() - > get('Articles');

// Start a new query.
$query = $articles - > find();
load more v
40%

Because the agregate query generated by the pagination class contain the having but not the select (distance),SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from properties having distance <= 500),Update: I've created a package for this: https://github.com/justbetter/laravel-pagination-with-havings,I have a query that return the cities on a fixed distance from a point:

$query - > addSelect(DB::Raw(
      '(3959 * acos (
      cos(radians('.$lat.')) *
      cos(radians(lat)) *
      cos(radians(lon) - radians('.$lon.')) +
      sin(radians('.$lat.')) *
      sin(radians(lat))
   )) AS distance ')) -
   > having('distance', '<', 300) - > paginate(10);
load more v

Other "undefined-undefined" queries related to "Laravel not executing queries with 'having' clause"