【问题标题】:Laravel 4.2 move pivot data into return model objectLaravel 4.2 将枢轴数据移动到返回模型对象中
【发布时间】:2015-09-09 10:51:54
【问题描述】:

我有一个从数据中返回的模型,看起来像这样,

    {
        "name" : "A test name",
        "data_due" : "2015/01/01",
        "user_id" : 1,
        "viewers": [
            {
                "id": 264,
                "email": "ainley87simon@gmail.com",
                "first_name": "",
                "last_name": "",
                "display_name": "",
                "initials": null,
                "active": null,
                "cost_visible": 0,
                "notification_frequency": "H",
                "pivot": {
                    "project_id": 667,
                    "user_id": 264,
                    "involved":1
                }
            }
       ]
 }

理想情况下,我想要返回以下内容,

        {
        "name" : "A test name",
        "data_due" : "2015/01/01",
        "user_id" : 1,
        "viewers": [
            {
                "id": 264,
                "email": "ainley87simon@gmail.com",
                "first_name": "",
                "last_name": "",
                "display_name": "",
                "initials": null,
                "active": null,
                "cost_visible": 0,
                "notification_frequency": "H",
                "involved":1,
                "pivot": {
                    "project_id": 667,
                    "user_id": 264,

                }
            }
       ]
 }

基本上我想要做的是将数据透视表中返回的相关列移动到主对象中。这是一个多对多关系,在 Project.php 模型中看起来像这样,

public function viewers() {
   return $this->belongsToMany('User', 'project_viewer')->withPivot('involved');
}

是否作为每个查看器对象的一部分而不是在数据透视表中返回?

【问题讨论】:

    标签: php laravel eloquent relational-database pivot-table


    【解决方案1】:

    我认为您无法将透视数据作为查询结果中的相关模型字段。 但是在您查询之后,有一些选项。

    您可以为数据透视字段定义访问器:

    在您的查看器模型中:

    function getInvolvedAttribute() 
    {
       return $this->pivot->involved;
    }
    

    您可以像这样访问它:

    $user = User::with('viewers')->find(1);
    foreach($user->viewers as $viewer) {
       echo $viewer->involved; // Here involved is available
    }
    

    另一种方法在您的查询结果上运行,并将其添加到您的模型中:

    $user = User::with('viewers')->find(1);
    foreach($user->viewers as $viewer) {
       $viewer->involved = $viewer->pivot->involved;
    }
    

    【讨论】:

      【解决方案2】:

      在Laravel Forums找到了更好的解决方案:

      <!-- language: lang-php -->
      <?php
      // Project model
      // override by aliasing the column to involved,
      // preventing it being attached to the pivot model
      public function viewers()
      {
          $this->belongsToMany('User', 'project_viewer')->withPivot('involved as involved');
      }
      
      // User model
      // To remove the pivot data in JSON, add this to the User model:
      protected $hidden = ['pivot'];
      
      // Execution
      $project->viewers->all();
      // [
      //   {
      //     "id": 264,
      //     "email": "ainley87simon@gmail.com",
      //     "first_name": "",
      //     "last_name": "",
      //     "display_name": "",
      //     "initials": null,
      //     "active": null,
      //     "cost_visible": 0,
      //     "notification_frequency": "H",
      //     "involved":1  
      //   },
      //   ...
      // ]
      

      我认为这个解决方案更简洁,为大多数用例删除了不必要的数据,并且只在需要时更容易访问数据透视属性。

      老问题,但也许它仍然会帮助一些程序员..

      【讨论】:

        猜你喜欢
        • 2018-03-05
        • 2018-11-12
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多