考虑两个模型A和B
A- > relatedTo B是一种one to one关系
使用(A - > hasOne- B)和(A - > belongsTo- B)有什么区别?
我可以互换使用吗?
即使在阅读文档后,也很难从相关的“属性”表中获取“属性名称”,同时又要从“产品”表中查询具有枢轴“ product_properties”的产品。
我的模型:Models\Product::class,Models\Property::class,Models\ProductProperty::class,
如果我运行查询:
$product = new App\Models\Product();
$product->where('id', 1)->with('properties')->get();
Run Code Online (Sandbox Code Playgroud)
我获得了完整的产品对象以及属性,并且效果很好,但是我不知道是如何从表中获取属性名称的properties。
总之,我需要显示与表相关的表中的属性名称。例如:130mm 宽度propertiesproduct_properties
我需要实现的是以下内容:
Illuminate\Database\Eloquent\Collection {#4400
all: [
App\Models\Product {
id: 1,
name: "Chrystal Block",
details: "Ships in an exclusive branded box.",
price: 50.00,
has_shipment: 1,
created_at: "2019-05-10 02:15:22",
updated_at: "2019-05-10 02:15:22",
deleted_at: null,
properties: Illuminate\Database\Eloquent\Collection {
all: [
App\Models\ProductProperty {
product_id: 4,
property_id: 1,
value: "130",
unit: "mm",
created_at: "2019-05-11 23:09:35", …Run Code Online (Sandbox Code Playgroud)