相关疑难解决方法(0)

我应该在Laravel中使用belongsTo还是hasOne?

考虑两个模型AB

A- > relatedTo B是一种one to one关系

使用(A - > hasOne- B)和(A - > belongsTo- B)有什么区别?

我可以互换使用吗?

laravel

46
推荐指数
2
解决办法
1万
查看次数

从数据透视表雄辩地获取数据

即使在阅读文档后,也很难从相关的“属性”表中获取“属性名称”,同时又要从“产品”表中查询具有枢轴“ product_properties”的产品。

我的模型:Models\Product::classModels\Property::classModels\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)

pivot-table relationship laravel eloquent

1
推荐指数
1
解决办法
81
查看次数

标签 统计

laravel ×2

eloquent ×1

pivot-table ×1

relationship ×1