Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
Php 在拉拉维尔elequent中使用拖缆_Php_Laravel_Eloquent_Many To Many_One To Many - Fatal编程技术网

Php 在拉拉维尔elequent中使用拖缆

Php 在拉拉维尔elequent中使用拖缆,php,laravel,eloquent,many-to-many,one-to-many,Php,Laravel,Eloquent,Many To Many,One To Many,在使用laravel 7时 假设我有三张桌子 +───────────+────────────────+────────+ | products | product_units | units | +───────────+────────────────+────────+ | id | id | id | | name | product_id | name | | ect | unit_id

在使用laravel 7时 假设我有三张桌子

+───────────+────────────────+────────+
| products  | product_units  | units  |  
+───────────+────────────────+────────+
| id        | id             | id     |  
| name      | product_id     | name   |  
| ect       | unit_id        |        |  
+───────────+────────────────+────────+
在产品模型中,我有这个方法

public function get_unit_relations()
{
    return $this->hasMany('App\Models\Product_unit','product_id','id');
}
public function get_unit_id()
{
    return $this->hasOne('App\Models\Unit','id','unit_id');
}
在产品单元内部,我有这个方法

public function get_unit_relations()
{
    return $this->hasMany('App\Models\Product_unit','product_id','id');
}
public function get_unit_id()
{
    return $this->hasOne('App\Models\Unit','id','unit_id');
}
现在,如果我想退货,我可以这样做

return Product::find(1);
所以我得到了这份回报

[
    {
        "id": 1,
        "name": "test",
    }
]
如果我想让返回有一个关系,我可以做这个代码

return Product::find(1)->with('get_unit_relations');
这将像这样返回json

[
    {
        "id": 1,
        "name": "test",
        "get_unit_relations": [
            {
                "id": 3,
                "unit_id": 2,
                "product_id": 1,
                "barcode": "455",
                "smallest_unit_relation": 12,
                "price": 12,
            }
        ]
    }
]
[
    {
        "id": 1,
        "name": "test",
        "get_unit_relations": [
            {
                "id": 3,
                "unit_id": 2,
                "product_id": 1,
                "barcode": "455",
                "smallest_unit_relation": 12,
                "price": 12,
                'get_unit_id':[ // how can i get the result with this method also to access the name of the unit
                    {
                        id:1,
                        name:'unit name',
                    }
                ]
            }
        ]
    }
]
我现在的问题是如何使用other->with
get\u unit\u relations
返回产品时->带('get\u unit\u relations')

为了得到这样的结果

[
    {
        "id": 1,
        "name": "test",
        "get_unit_relations": [
            {
                "id": 3,
                "unit_id": 2,
                "product_id": 1,
                "barcode": "455",
                "smallest_unit_relation": 12,
                "price": 12,
            }
        ]
    }
]
[
    {
        "id": 1,
        "name": "test",
        "get_unit_relations": [
            {
                "id": 3,
                "unit_id": 2,
                "product_id": 1,
                "barcode": "455",
                "smallest_unit_relation": 12,
                "price": 12,
                'get_unit_id':[ // how can i get the result with this method also to access the name of the unit
                    {
                        id:1,
                        name:'unit name',
                    }
                ]
            }
        ]
    }
]
我想要的是返回产品,并且有许多产品单元,只有一个单元可以访问该单元的名称
非常感谢。

我找到了这样添加
get\u unit\u id
的解决方案

public function get_unit_relations()
{
    return $this->hasMany('App\Models\Product_unit','product_id','id')->with('get_unit_id');
}
谢谢大家

我想
使用('get\u unit\u relations','get\u unit\u relations.get\u unit\u id')->…
就可以了