我有下表并想要獲取產品的所有單元(基本單元和子單元)
單位
| ID | 姓名 | 乘數 | base_unit_id |
|---|---|---|---|
| 1 | 片 | 1 | 無效的 |
| 2 | 打 - 12 | 12 | 1 |
產品
| ID | 姓名 | 成本 | 價格 | unit_id |
|---|---|---|---|---|
| 1 | 產品1 | 10 | 14 | 1 |
| 1 | 產品2 | 10 | 14 | 1 |
產品模型內部的關系
public function units()
{
return $this->belongsTo(Unit::class, 'unit_id', 'id'); // Or where base_unit_id = product unit id
// I have tried this
// $this->belongsTo(Unit::class, 'unit_id', 'id')->orWhere('base_unit_id', $this->unit_id)
// Does not work
}
我喜歡這樣的產品
$products = Product::with('units')->get();
預期輸出為
[
{
"id": 1,
"name": "Product 1",
"unit_id": 1,
"cost": 10,
"price": 14,
"units": [
{
"id": 1,
"name": "Piece",
"multiplier": 1,
"base_unit_id": null
},
{
"id": 1,
"name": "Dozen - 12",
"multiplier": 12,
"base_unit_id": 1
}
]
},
{
"id": 1,
"name": "Product 2",
"unit_id": 1,
"cost": 10,
"price": 14,
"units": [
{
"id": 1,
"name": "Piece",
"multiplier": 1,
"base_unit_id": null
},
{
"id": 1,
"name": "Dozen - 12",
"multiplier": 12,
"base_unit_id": 1
}
]
}
]
我想要一個串列中的單位,我該怎么做?
uj5u.com熱心網友回復:
這是我找了一段時間后想出的解決方案。
產品型號
public function baseUnit()
{
return $this->belongsTo(Unit::class, 'unit_id', 'id');
}
單元模型
public function subUnits()
{
return $this->hasMany(Unit::class, 'base_unit_id', 'id');
}
你可以這樣稱呼它
$product = Product::with(['baseUnit.subUnits'])->get();
哪個讓你得到這個輸出
[
{
"id": 1,
"name": "Product 1",
"unit_id": 1,
"cost": 10,
"price": 14,
"base_unit": {
"id": 1,
"name": "Piece",
"multiplier": 1,
"base_unit_id": null,
"sub_units": [
{
"id": 1,
"name": "Dozen - 12",
"multiplier": 12,
"base_unit_id": 1
}
]
}
}
]
您可以在Product Model中創建一個輔助函式來格式化這樣的單位。
public function units()
{
$baseUnit = $this->baseUnit->replicate(); // Clone it so we don't modify the original
$baseUnit->id = $this->baseUnit->id; // Set the id to the original unit id
$result = collect();
$baseUnit->subUnits->each(function ($unit) use (&$result) {
$result->push($unit);
});
$baseUnit->offsetUnset('subUnits');
$result->push($baseUnit);
unset($baseUnit);
return $result;
}
輸出
[
{
"id": 1,
"name": "Dozen - 12",
"multiplier": 12,
"base_unit_id": 1
},
{
"id": 1,
"name": "Piece",
"multiplier": 1,
"base_unit_id": null
}
]
感謝所有花時間回答我的人。
uj5u.com熱心網友回復:
您將需要通過資料透視表在Product和Unit模型之間實作多對多關系
基本上就像:產品記錄可以有很多單元記錄,單元記錄可以有很多產品記錄 -多對多
public function up()
{
Schema::create('product_unit', function(Blueprint $table) {
$table->foreignId('product_id')->constrained('products')->onDelete('cascade');
$table->foreignId('unit_id')->constrained('units')->onDelete('cascade');
$table->timestamps();
$table->primary(['product_id', 'uint_id']);
});
}
public function down()
{
Schema::dropIfExists('product_unit');
}
接下來你需要定義關系
//Product model
public function units()
{
return $this->belongsToMany(Unit::class)->withTimestamps();
}
//Unit model
public function products()
{
return $this->belongsToMany(Product::class)->withTimestamps();
}
Laravel Docs - 雄辯的關系 - 多對多
Laravel Docs - 雄辯的關系 - 更新多對多關系
uj5u.com熱心網友回復:
沒有簡單的方法可以做到這一點。
我的解決方案是使用getAttribute:
public function getUnitsAttribute()
{
Unit::where('id', $this->unit_id)->orWhere('base_unit_id')->get();
}
但無論如何,您不能使用關系方法(如with()or load()),也不能像查詢構建器一樣使用它。
此外,您可以試試這個:https ://stackoverflow.com/a/29775608/19262677 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485952.html
