1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to get the root relation id in a nested relationship

Discussão em 'Outras Linguagens' iniciado por Muntasir Hasan, Outubro 9, 2024 às 07:52.

  1. Product::with(['sub_products' => function ($query) {
    $query->with(['product' => function ($subQuery) use ($query){
    $subQuery->with(['sub_products' => function ($nestedQuery) use ($query) {
    $nestedQuery->where('main_product_id', $query->getModel()->id); // top product condition
    $nestedQuery->with(['product']);
    }]);
    }]);
    }]);


    Here $query->getModel()->id is not returning the top product id

    How can i get the top relation id inside the nested relation.


    table: products: id, name;


    table: product_relations: id, main_product_id, parent_product_id, child_product_id;

    public function sub_products()
    {
    return $this->hasMany(ProductRelation::class, 'parent_product_id');
    }


    i tried various ways but everywhere it ask to pass $main_product_id statically.

    Thanks

    Continue reading...

Compartilhe esta Página