【问题标题】:Laravel 4.1 remove pivot attributes from responseLaravel 4.1 从响应中删除枢轴属性
【发布时间】:2014-01-02 16:44:29
【问题描述】:

我正在使用 laravel 4.1 构建一个 api。我有一个工作正常的表。但是响应带有我不想要的枢轴属性。正如您将在我的示例中看到的那样,我必须有两个表名:trips 和 users。我不想在我的回复中看到数据透视表属性。这是一个例子:

[
    {
        "id": 140,
        "name_first": "hasan",
        "name_last": "hasibul",
        "profile_image": "/assets/images/default-profile-img.png",
        "created_at": "2013-09-18 08:19:50",
        "last_login": "2013-12-26 11:28:44",
        "status": "active",
        "last_update": "2013-10-15 13:40:47",
        "google_refresh_token": null,
        "is_admin": 1,
        "updated_at": null,
        "pivot": {
            "trip_id": 200,
            "user_id": 140
        }
    }

这是我的用户模型:

public function trips(){
        return $this->belongsToMany('Trip');
    }

这是我的旅行模型:

public function users(){
        return $this->belongsToMany('User');
    }

这是我的控制器:

public function index($tripId)
    {
        $userCollection = Trip::find($tripId)->users;
        return $userCollection;
    }

这是我的路线:

//get all the users belongs to the trip
Route::get('trips/{tripId}/users', array(
    'as' => 'trips/users/index',
    'uses' => 'TripUserController@index'
));

有什么方法可以使用 laravel 删除数据透视属性,或者我必须使用 php 吗?

【问题讨论】:

  • 这取决于您如何生成此结果。请出示相关代码。

标签: laravel-4


【解决方案1】:

使用模型的$hidden属性,你可以给它添加属性或关系,pivot基本上就是一个关系。

class Foo extends Eloquent
{
    protected $hidden = array('pivot');

    public function bars()
    {
        return $this->belongsToMany('Bar');
    }
}

【讨论】:

  • @TLGreg 如何仅隐藏关键的特定字段?比如说,我的关键字段中有a_id、b_id 和dummy 字段,我只想从这三个字段中检索dummy。有什么办法吗?
  • @VishalSharma 你知道怎么回答你的问题吗?
【解决方案2】:

如果您只想从响应中删除任何一列,那么您可以尝试以下操作:

在你的模型中:

public function toArray()
{
    $attributes = $this->attributesToArray();
    $attributes = array_merge($attributes, $this->relationsToArray());
    unset($attributes['pivot']['user_id']);
    return $attributes;
}

这样您将只获得所需的属性。

【讨论】:

    【解决方案3】:

    您可以将它添加到您的“隐藏”数组中。在模型页面

    protected $hidden = [
        'pivot'
    ];
    

    【讨论】:

      【解决方案4】:

      如上所述,您可以通过将以下内容添加到相关模型中来从响应中删除枢轴属性。

          protected $hidden = [
              'pivot'
          ];
      
      

      此外,如果您想从枢轴中选择特定字段以显示在相关用户对象中,您可以使用 Laravel 5.8 将其添加到控制器中。当您使用上述代码 sn-p 隐藏数据透视信息时,这也有效。

      public function index(Trip $trip)
      {
          return $trip->users()->select(['trip_id'])->paginate();
      }
      
      

      你会收到一些对象,其中trip_id被添加到用户对象中。

          {
              "data": [
               {
                      "id": 140,
                      "trip_id": 200,
                      "name_first": "hasan",
                      "name_last": "hasibul",
                      "profile_image": "/assets/images/default-profile-img.png",
                      "created_at": "2013-09-18 08:19:50",
                      "last_login": "2013-12-26 11:28:44",
                      "status": "active",
                      "last_update": "2013-10-15 13:40:47",
                      "google_refresh_token": null,
                      "is_admin": 1,
                      "updated_at": null,
                      "pivot": {
                          "trip_id": 200,
                          "user_id": 140
                      }
                  }
              ]
          }
      

      【讨论】:

        猜你喜欢
        • 2017-09-30
        • 2017-04-24
        • 1970-01-01
        • 2017-08-17
        • 2017-11-18
        • 2016-10-20
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多