Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 在模型类别和子类别中使用相同的作用域与即时加载的关系_Laravel_Scope_Relationship_Categories_Eager Loading - Fatal编程技术网

Laravel 在模型类别和子类别中使用相同的作用域与即时加载的关系

Laravel 在模型类别和子类别中使用相同的作用域与即时加载的关系,laravel,scope,relationship,categories,eager-loading,Laravel,Scope,Relationship,Categories,Eager Loading,我对嵌套类别使用kalnoy/nestedset 现在,我想列出具有子体的类别,我想将scope addSelect查询添加到所选类别和子体中,以从订单关系中获取销售额 我还过滤模型和子体关系 这是我的控制器 $categories = $this->repository ->scopeQuery(function ($query) use ($request) { return $query ->wit

我对嵌套类别使用kalnoy/nestedset 现在,我想列出具有子体的类别,我想将scope addSelect查询添加到所选类别和子体中,以从订单关系中获取销售额

我还过滤模型和子体关系

这是我的控制器

 $categories = $this->repository
        ->scopeQuery(function ($query) use ($request) {
            return $query
                ->with(['descendantsWithCount' => function($query) use ($request){
                    $query->filtered($this->categoryFilter->handle($request));
                }])
                ->with('products', 'trend', 'descendantsWithCount.trend')
                ->sortable();
        })
        ->totalOrdersCount()
        ->totalOrdersPrice()
        ->filtered($this->categoryFilter->handle($request))
        ->paginate(15);

    ddd($categories);
我试图创建另一个关系以将作用域添加到其中,但仍然没有得到正确的结果—它得到了父结果

这里是我添加的新关系

public function descendantsWithCount()
{
    return $this->descendants()->totalOrdersCount()->totalOrdersPrice();
}
并尝试在闭包中添加作用域,如下所示:

$categories = $this->repository
        ->scopeQuery(function ($query) use ($request) {
            return $query
                ->with(['descendants' => function($query) use ($request){
                    $query->filtered($this->categoryFilter->handle($request))
                        ->totalOrdersCount()
                        ->totalOrdersPrice();
                }])
                ->with('products', 'trend', 'descendants.trend')
                ->sortable();
        })
        ->totalOrdersCount()
        ->totalOrdersPrice()
        ->filtered($this->categoryFilter->handle($request))
        ->paginate(15);

    ddd($categories);
但是我没有在关系中添加任何字段

这是我的望远镜

public function scopeTotalOrdersCount($query)
{
    $query->addSelect([
        'total_orders_count' => Order::select(
            DB::raw('COUNT(*) as count')
        )->whereIn('id', function($query){
            $query->select('order_id')
                ->from('order_items')
                ->whereIn('product_id', function($query){
                    $query->select('product_id')
                            ->from('category_product')
                            ->whereIn('category_id', function($query){
                                $query->select('id')
                                    ->from('categories as c')
                                    ->whereColumn('_lft','>=' ,'categories._lft')
                                    ->whereColumn('_rgt','>=' ,'categories._lft');
                            });
                });
        })->where('status', "=", 5)
    ]);
}

public function scopeTotalOrdersPrice($query)
{
    $query->addSelect([
        'total_orders_price' => Order::select(
            DB::raw('COALESCE(SUM(total_price), 0) as total_price_sum')
        )->whereIn('id', function($query){
            $query->select('order_id')
                ->from('order_items')
                ->whereIn('product_id', function($query){
                    $query->select('product_id')
                            ->from('category_product')
                            ->whereIn('category_id', function($query){
                                $query->select('id')
                                    ->from('categories as c')
                                    ->whereColumn('_lft','>=' ,'categories._lft')
                                    ->whereColumn('_rgt','>=' ,'categories._lft');
                            });
                });
        })->where('status', "=", 5)
    ]);
}