using tow with in laravel eloquent
using tow with in laravel elequent
正在使用 laravel 7
假设我有三个 table
+───────────+────────────────+────────+
| 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');
}
在product_units里面我有这个方法
public function get_unit_id()
{
return $this->hasOne('App\Models\Unit','id','unit_id');
}
现在如果我想要 return 产品我可以做到..
return Product::find(1);
所以我得到了这个 return
[
{
"id": 1,
"name": "test",
}
]
如果我希望 return 有关系,我可以执行此代码
return Product::find(1)->with('get_unit_relations');
这将 return json 像这样..
[
{
"id": 1,
"name": "test",
"get_unit_relations": [
{
"id": 3,
"unit_id": 2,
"product_id": 1,
"barcode": "455",
"smallest_unit_relation": 12,
"price": 12,
}
]
}
]
我现在的问题是如何将其他 ->with 与 get_unit_relations
一起使用
当 return 产品->with('get_unit_relations')
public function get_unit_id()
{
return $this->hasOne('App\Models\Unit','id','unit_id');
}
得到这样的结果..
[
{
"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',
}
]
}
]
}
]
我想要的是return产品和hasMany product_units和hasOne单元来访问单元名称
非常感谢..
我找到了这样添加 get_unit_id
的解决方案
public function get_unit_relations()
{
return $this->hasMany('App\Models\Product_unit','product_id','id')->with('get_unit_id');
}
它的工作感谢大家..
正在使用 laravel 7 假设我有三个 table
+───────────+────────────────+────────+
| 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');
}
在product_units里面我有这个方法
public function get_unit_id()
{
return $this->hasOne('App\Models\Unit','id','unit_id');
}
现在如果我想要 return 产品我可以做到..
return Product::find(1);
所以我得到了这个 return
[
{
"id": 1,
"name": "test",
}
]
如果我希望 return 有关系,我可以执行此代码
return Product::find(1)->with('get_unit_relations');
这将 return json 像这样..
[
{
"id": 1,
"name": "test",
"get_unit_relations": [
{
"id": 3,
"unit_id": 2,
"product_id": 1,
"barcode": "455",
"smallest_unit_relation": 12,
"price": 12,
}
]
}
]
我现在的问题是如何将其他 ->with 与 get_unit_relations
一起使用
当 return 产品->with('get_unit_relations')
public function get_unit_id()
{
return $this->hasOne('App\Models\Unit','id','unit_id');
}
得到这样的结果..
[
{
"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',
}
]
}
]
}
]
我想要的是return产品和hasMany product_units和hasOne单元来访问单元名称 非常感谢..
我找到了这样添加 get_unit_id
的解决方案
public function get_unit_relations()
{
return $this->hasMany('App\Models\Product_unit','product_id','id')->with('get_unit_id');
}
它的工作感谢大家..