Laravel 有许多多对多对一雄辩

2024-03-18

我没有太多运气以 Laravel 的方式解决这个问题。所以我提出两个问题。

假设我有一辆汽车,并且该汽车可以有许多特征,但该特征也按特征类型分隔,我如何返回所述汽车的所有特征,分隔特征类型?

我有四个表,listings_features 是数据透视表:

  • 列表(ID)
  • 清单_功能(清单_id,功能_id)
  • Listings_features_types(id,类型)
  • listings_features_values(id,listing_feature_type_id,值)

我有以下代码,它生成我需要的内容,但是当我使用它时,我收到 Laravel 错误...“调用字符串上的成员函数 addEagerConstraints()”...因为我这样调用它:

Listing::with(
        'features',
    )->get();

我用来以我想要的格式(不坚定)生成数据的代码是

public function features()
{
    $out = array();
    $features = $this->hasMany('App\Models\ListingFeature', 'listing_id', 'id')->select('feature_id')->get();
    $types = ListingFeatureType::all();
    foreach($types as $key => $obj){
        $out[$key]['listing_feature_type_id'] = $obj->id;
        $out[$key]['name'] = $obj->listing_feature_type;
        $out[$key]['features'] = ListingFeatureValue::whereIn('id', $features->toArray())->where('listing_feature_type_id', '=', $obj->id)->get()->toArray();
    }
    return json_encode($out);
}

返回:

[
  {
    "listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
    "name": "Safety",
    "features": [
      {
        "id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9",
        "listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
        "listing_feature_text": "Anti-Lock Brakes"
      },
      {
        "id": "37abeef2-dc22-4995-8503-f89962242ea6",
        "listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
        "listing_feature_text": "Collision Detection"
      },
      {
        "id": "3c0728e1-91f7-4f44-ac0b-429eda816692",
        "listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
        "listing_feature_text": "Dual Airbags"
      },
      {
        "id": "4255b8b4-e71c-4059-8a22-1b9894512564",
        "listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
        "listing_feature_text": "Side Airbags"
      }
    ]
  },
  {
    "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
    "name": "Interior",
    "features": [
      {
        "id": "1b89581e-1a30-4dce-9455-ab0ad4c49bcf",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "Privacy Glass"
      },
      {
        "id": "59e3628f-3cef-4447-9cb2-71be4a3046a4",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "Onboard GPS"
      },
      {
        "id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "In-dash Navigation"
      },
      {
        "id": "8fe836a3-5596-4306-aac1-bae4cb596e20",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "Power Windows"
      },
      {
        "id": "e4addb5a-1b26-4ae3-b0ee-3b8bce892fb9",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "Tinted Windows"
      },
      {
        "id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
        "listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
        "listing_feature_text": "CD Player"
      }
    ]
  },
  {
    "listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
    "name": "Exterior",
    "features": [
      {
        "id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
        "listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
        "listing_feature_text": "Spoiler"
      }
    ]
  }
]

以下是模型(非常基本,刚刚开始):

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ListingFeatureValue extends Model
{

    protected $table = 'listings_features_values';
    public $timestamps = false;
    public $incrementing = false;

    public function type() {
        return $this->belongsTo('App\Models\ListingFeatureType', 'listing_feature_type_id', 'id');
    }

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ListingFeatureType extends Model
{

    protected $table = 'listings_features_types';
    public $timestamps = false;
    public $incrementing = false;

    public function values() {
        return $this->hasMany('App\Models\ListingFeatureValue', 'listing_feature_type_id', 'id');
    }

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ListingFeature extends Model
{

    protected $table = 'listings_features';
    public $timestamps = false;
    public $incrementing = false;

}

如何创建 Laravel 模型关系来实现相同的结果集,但如上所述调用它?或者,我怎样才能像上面那样调用它但阻止错误发生?

如果您做到了这一点,谢谢!

Update:

我无法让它按原样工作,我收到了一个 SQL 错误,这让我觉得我需要一个belongsToManyThrough,这样我就可以指定表名称:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ad_l5.listing_listing_feature' doesn't exist (SQL: select `listings_features`.*, `listing_listing_feature`.`listing_id` as `pivot_listing_id`, `listing_listing_feature`.`listing_feature_id` as `pivot_listing_feature_id` from `listings_features` inner join `listing_listing_feature` on `listings_features`.`id` = `listing_listing_feature`.`listing_feature_id` where `listing_listing_feature`.`listing_id` in (b266c874-1cef-4f49-b65f-f91ddaaf6aee, e93674ca-3f82-45d8-9961-e8569cac164b))

但是使用@Carter Fort 发布的确切代码作为下面的答案,并将 features() 方法更改为

return $this->belongsToMany(ListingFeatureValue::class, 'listings_features', 'listing_id', 'feature_id');

还添加到模型 ListingFeatureValue

public function type()
    {
        return $this->belongsTo(ListingFeatureType::class, 'listing_feature_type_id', 'id');
    }

我得到的输出:

"featuresByType": {
"": [
{
"id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text": "Spoiler",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1"
},
"type": {
"id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_type": "Exterior"
}
},
{
"id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "CD Player",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "In-dash Navigation",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "66fe416b-98dc-45c8-979d-78f2ea7fe876"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Anti-Lock Brakes",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9"
},
"type": {
"id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_type": "Safety"
}
},
  ...

这比我当时的情况要好,但如果结果按类型分组,然后按其中的功能分组,而不是每个功能都附加一个类型,那就太好了。使得解析显示变得更加困难。感谢迄今为止的帮助!

Update 2

这些将是我的架构文件:

// listings
Schema::create('listings', function (Blueprint $table) {
    $table->string('id');
    $table->string('title');
});

// listings to features pivot
Schema::create('listings_features', function (Blueprint $table) {
    $table->string('listing_id');
    $table->string('feature_id');
});

// feature types (i.e. Safety)
Schema::create('listings_features_types', function(Blueprint $table){
    $table->string('id');
    $table->string('listing_feature_type');
});

// feature values (i.e. Anti-Lock Brakes)
Schema::create('listings_features_values', function(Blueprint $table){
    $table->string('id');
    $table->string('listing_feature_type_id'); // links to listings_features_types
    $table->string('listing_feature_text');
});

Update 3根据下面的答案进行更改(一旦工作,我将发布所有代码)我接近我想要的。

"features": [
{
"id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text": "Spoiler",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1"
},
"type": {
"id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_type": "Exterior"
}
},
{
"id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "CD Player",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "In-dash Navigation",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "66fe416b-98dc-45c8-979d-78f2ea7fe876"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}

// .... ]

我想要的是:

{
   "feature_types":[
      {
         "type":{
            "id":"8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
            "listing_feature_type":"Interior",
            "features":[
               {
                  "id":"3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
                  "listing_feature_type_id":"8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
                  "listing_feature_text":"GPS"
               },
               {
                  "id":"66fe416b-98dc-45c8-979d-78f2ea7fe876",
                  "listing_feature_type_id":"84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
                  "listing_feature_text":"In-dash Navigation"
               },

            ]            "pivot":{
               "listing_id":"e93674ca-3f82-45d8-9961-e8569cac164b",
               "feature_id":"f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
            }
         }
      }
   ]
}

您可以在之间使用多对多关系ListingListingFeatureValue模型,然后使用给定列表按其类型对相关列表功能进行分组groupBy采集方法。

The Listing model:

class Listing extends Model {

    protected $hidden = [
        'features'
    ];

    protected $appends = [
        'feature_types'
    ];

    public function features(){
        return $this->belongsToMany(ListingFeatureValue::class, 'listings_features', 'listing_id', 'feature_id');
    }

    public function getFeatureTypesAttribute()
    {
        return $this->features->groupBy(function ($feature, $key) {
            return $feature->type->id;
        })->map(function($features, $key){
            $type = ListingFeatureType::find($key);
            $type->features = $features;
            return $type;
        })->values();
    }

}

The getFeatureTypesAttribute()是这里的明星,因为你可以将其与appends数组以强制 Eloquent 模型将其附加到任何toArray()调用模型实例,这就是toJson()将模型转换为 JSON 时使用。

首先获取所有列表值然后使用groupBy and map收集方法,但没有原生的 Eloquent 机制可供使用hasManyThrough通过多对多关系。有一个这里有不同的方法 https://stackoverflow.com/questions/23788844/hasmanythrough-with-one-to-many-relationship/23789210#23789210如果你不喜欢这个。

The 列表特征值 model:

class ListingFeatureValue extends Model
{
    public $table = 'listings_features_values';

    public function type()
    {
        return $this->belongsTo(ListingFeatureType::class, 'feature_type_id');
    }
}

我在这里展示这个模型是因为type()关系被称为getFeaturesByTypeAttribute()上面的方法,不希望有任何混乱。

而且,只是为了完整起见,列表功能类型 model:

class ListingFeatureType extends Model
{
    public $table = "listings_features_types";

    public function listings()
    {
        return $this->hasMany(ListingFeatureValue::class, 'listing_feature_type_id');
    }
}

如果您想立即加载列表及其功能和类型以获得所有列表的完整输出,您可以这样做:

App\Listing::with('features.type')->get()->toJson();

我的迁移文件如下所示:

//create_listings_table
Schema::create('listings', function (Blueprint $table) {
    $table->increments('id');
    $table->string('uuid');
    $table->timestamps();
});

//create_listings_features_values_table
Schema::create('listings_features_values', function (Blueprint $table) {
    $table->increments('id');
    $table->string('listing_feature_text');
    $table->integer('listing_feature_type_id')->unsigned();
    $table->timestamps();
});

//create_listings_features_types_table    
Schema::create('listings_features_types', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

//create_listings_features_table
Schema::create('listings_features', function(Blueprint $table){
    $table->integer('listing_id')->unsigned();
    $table->integer('listing_feature_id')->unsigned();
});

您可以在此处了解有关 Collection 方法的更多信息:

https://laravel.com/docs/5.3/collections#available-methods https://laravel.com/docs/5.3/collections#available-methods

...并在这里急切加载:

https://laravel.com/docs/5.3/eloquent-relationships#eager-loading https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

...以及这里的多对多关系:

https://laravel.com/docs/5.3/eloquent-relationships#many-to-many https://laravel.com/docs/5.3/eloquent-relationships#many-to-many

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 有许多多对多对一雄辩 的相关文章

随机推荐

  • 将 gcc libs .data 放在特定部分?

    我正在尝试为我们的嵌入式系统切换到 GNU GCC 编译器 但由于我们芯片的内存布局被分割 我在链接该项目时遇到了问题 RAM section 1 0x10000 0x12FFF RAM section 2 0x18000 0x1BFFF
  • Express js中通过id删除mongodb文档

    我正在为此抓狂 尝试通过 id 删除文档 router delete api menu delete id function req res var id req params id db get collection menu funct
  • 如何在自定义验证属性中获取/注入服务

    我们使用 NET Core 3 1 5 这是一个 Blazor 服务器应用程序 我们有一个 ValidationAttribute 并需要访问外部服务来验证对象 ValidationAttribute 有 IsValid 方法 protec
  • 有没有办法在 C# 中修改进程 DACL

    我有更改进程 DACL 的遗留 C 代码 并尝试使用 NET 3 5 中的托管代码类 我在网上找到了代码 其中有人创建了一个 SetAclOnServices 类 该类扩展了服务的 NativeObjectSecurity 类 我认为我可以
  • jQuery 数据表中的列排序

    我已经了解了 jQuery 数据表插件中的列排序以及控制它的各种方法 我有一个查询是否可以通过单击上箭头图标将按升序和下箭头图标进行排序的方式控制排序会按降序排序吗 有两种方法可以做到这一点 具体取决于datatables版本 编辑数据表版
  • 如何在Python中将一个对象的所有属性复制到另一个对象?

    在Python中 是否有一个库方法可以复制同一类的两个 已经存在的 实例之间的所有属性 我的意思是 类似 Apache Commons 的东西PropertyUtilsBean copyProperties Try destination
  • ActiveRecord 查询比直接 SQL 慢得多?

    我一直致力于优化项目的数据库调用 我注意到下面两个相同的调用之间的性能存在 显着 差异 connection ActiveRecord Base connection pgresult connection execute SELECT S
  • 当我“推”到 Bitbucket 时,可以从 VPS 中“拉”一个钩子

    我正在 Bitbucket 中管理我的 WordPress 模板 每次我push主题 我必须登录我的 VPS 服务器并且pull回购协议 我想自动完成 如果我自己运行 git daemon 我找到了一个解决方案 推送到服务器后执行自动拉取请
  • 基于现有模板生成Word文档的最佳方法是什么

    TL DR 我可以使用 NET 生成 Word 文档 如 XAML ItemTemplates 吗 我发现很难找到一个满足我所有要求的解决方案 所以我想我会把它扔到 stackoverflow 希望有人能指导我 非常感谢 简单来说 我需要根
  • 如何根据条目的长度过滤 pandas 数据框

    在 pandas 数据框中 我有一个字段 amp 应由长度为 495 的列表填充 是否有一种 pandas ic 方法可以快速过滤此长度 使得具有字段 amp 的所有行不等于 495被丢弃 I tried df len df amp 495
  • 如何将 SQL 查询与不同的表达式结合起来?

    我的三个查询已经达到了我的 SQL 知识的顶峰 Microsoft SQL 2005 如果这很重要 现在我需要将它们组合成一个查询 并将所有值放在一行上 我的实际查询如下 但我认为如果我在这里提供一个简单的版本会更容易 查询一 Provid
  • 尝试覆盖 Hive 分区时写入 __HIVE_DEFAULT_PARTITION__ 的行已损坏

    当尝试使用 Spark 2 3 覆盖 Hive 表中的分区时 我看到一些非常奇怪的行为 首先 我在构建 SparkSession 时设置以下设置 config spark sql sources partitionOverwriteMode
  • XmlPullParser:获取包含 XML 标签的内部文本

    假设您有一个如下所示的 XML 文档
  • 有什么方法可以使用 boto3 直接将文件写入 S3 吗?

    我编写了一个 python 脚本来处理非常大的文件 总共几 TB 我将在 EC2 实例上运行该脚本 之后 我想将处理后的文件存储在 S3 存储桶中 目前 我的脚本首先将数据保存到磁盘 然后将其上传到S3 不幸的是 考虑到等待实例首先写入磁盘
  • Google PageSpeed 用户代理的名称是什么?

    这一页 https developers google com speed docs insights faq whyonlinediff表示它使用自定义用户代理 但没有提及名称 如果用户代理来自 PageSpeed 我希望能够运行一些逻辑
  • VS2013测试代理和控制器不通信

    我有一个运行 VS2013 负载测试控制器的 Windows Azure VM 和另一个运行 2013 负载测试代理的 Azure VM 我一直无法让两人成功沟通 我将每个虚拟机的主机名和 IP 添加到另一个虚拟机的 HOSTS 文件中 我
  • 依赖注入上下文中的组合根是什么?

    我正在探索依赖注入和术语成分根到处都在使用 那么它是什么 组合根是应用程序中使用依赖项注入容器组合应用程序的对象图的单个位置 尽管如何完成此操作无关紧要 它可以使用容器 也可以使用手动完成 pure DI http blog ploeh d
  • 是否可以在 HTML 中创建多级有序列表? [复制]

    这个问题在这里已经有答案了 我要这个 1 Main 1 1 sub1 1 2 sub2 2 Main2 2 1 sub3 可以在 HTML 中做到这一点吗 谢谢 这个解决方案对我有用 hide original list counter o
  • 我什么时候使用像 Paxos 这样的共识算法,什么时候使用像向量时钟这样的算法?

    我已经阅读了很多有关保证分布式系统中节点之间一致性的不同策略的文章 但我在弄清楚何时使用哪种算法时遇到了一些麻烦 我会在什么样的系统中使用矢量时钟之类的东西 哪个系统最适合使用 Paxos 之类的东西 两者是互相排斥的吗 有一个由 2 个节
  • Laravel 有许多多对多对一雄辩

    我没有太多运气以 Laravel 的方式解决这个问题 所以我提出两个问题 假设我有一辆汽车 并且该汽车可以有许多特征 但该特征也按特征类型分隔 我如何返回所述汽车的所有特征 分隔特征类型 我有四个表 listings features 是数