A
A
Alexander Smirnov2020-12-14 21:25:05
Laravel
Alexander Smirnov, 2020-12-14 21:25:05

Why can't I use where, but only whereRaw?

I am writing a query to a POSTGRESQL database with two JOINs, I am seeing strange magic. whereInIf I use or anywhere in the query where, then POSGRESQL swears something like this:

ERROR:  bind message supplies 3 parameters, but prepared statement \"pdo_stmt_00000009\" requires 4


If I manually rewrite to whereRaw, then the error goes away and everything works as expected. Of course, you can live with using whereRaw, but still I would like to understand the reason for this behavior

Versions
of Laravel Framework 7.28.4
PostgreSQL 13.1

Query code (working version):

$table = DB::
    table("stocks")->
    select("products.product_id")->
    selectRaw("SUM(stocks.quantity_full) AS quantity_full")->
    selectRaw("SUM(stocks.in_way_to_client) AS in_way_to_client")->
    selectRaw("SUM(stocks.in_way_from_client) AS in_way_from_client")->
    rightJoin("sizes", "sizes.barcode", "=", "stocks.size_id")->
    rightJoin("products", function($join) use($brands, $subject) {
        return $join->
            on("sizes.product_id", "=", "products.product_id")->
            whereRaw("products.brand IN (".implode($b.")")->
            when($subject > 0, function($query) use($subject) {
                return $query->whereRaw("products.subject_id = ". $subject);
            });
    })->
    groupBy("products.product_id");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Smirnov, 2020-12-21
@sasha-hohloma

The answer turned out to be quite simple - you need to read the documentation
more carefully. The problem was the use of the specified request inside another request via leftJoin. Instead, it was necessary to useleftJoinSub

P
pLavrenov, 2020-12-16
@pLavrenov

Because in where such constructions "products.brand" do not work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question