staudenmeir / laravel-cte

Laravel queries with common table expressions
MIT License
512 stars 38 forks source link

Argument Type Mismatch in JoinClause::__construct with PostgresConnection in LaravelCte Package #58

Closed TursunboyevJahongir closed 3 days ago

TursunboyevJahongir commented 2 weeks ago

telegram-cloud-photo-size-2-5370892574455357372-y

I'm encountering an issue where an argument type mismatch occurs when using the JoinClause in conjunction with the PostgresConnection provided by the staudenmeir/laravel-cte package. The error message is as follows: Returned error:

    "message": "Illuminate\\Database\\Query\\JoinClause::__construct(): Argument #1 ($parentQuery) must be of type Illuminate\\Database\\Query\\Builder, Staudenmeir\\LaravelCte\\Connections\\PostgresConnection given, called in /Users/jahongir/Projects/daftar_new/vendor/laravel/framework/src/Illuminate/Database/Query/JoinClause.php on line 144",
    "exception": "TypeError",
    "file": "/Users/jahongir/Projects/daftar_new/vendor/laravel/framework/src/Illuminate/Database/Query/JoinClause.php",
    "line": 59,

Here’s the code snippet causing the issue:

        $products = BmStock::filter($this->filter($data))
            ->join(config('database.schemas.static') . '.product_relations',
                function (JoinClause $join) use ($product) {
                    $join->on('product_relations.focus_product_id', '=', 'bm_stocks.focus_product_id')
                        ->join(config('database.schemas.static') . '.products',
                            function (JoinClause $join) use ($product) {
                                $join->on('product_relations.product_id', '=', 'products.id')
                                    ->where(function (QueryBuilder $query) use ($product) {
                                        if ($product->variation_id !== null) {
                                            $query->orWhere('products.variation_id', $product->variation_id);
                                        }
                                        $query->where('products.id', $product->id);
                                    });
                            });
                })

Environment:

Laravel Version: 11 PHP Version: 8.3 Database Driver & Version: PostgreSQL16 Package Version: staudenmeir/laravel-cte 1.11.1

staudenmeir commented 2 weeks ago

Hi @TursunboyevJahongir, This error is not caused by the package: The query also fails when you run it on a fresh Laravel installation.

The issue is that you are trying to nest join clauses. What should the generated SQL look like?