JSON返回过多信息

时间:2019-02-27 23:09:02

标签: php json database laravel

我正在使用Laravel。

我也不是很好地解释...问题是我试图从数据库中获取一些值。

路线:

Route::post('inbox/all', 'InboxController@message_type_1');

我的控制器:

public function message_type_1(Request $request)
{
  $user_id =  Auth::user()->id;
  $result =   Messages::where('receiver_id', $user_id)->with('sender')->with(['bookings' => function($query) {
    $query->with('currency');
  }])->with('object_address')->orderBy('id','desc');

  $result =  $result->paginate(10)->toJson();
  return $result;
}

退货给了我很多信息,有些我不愿意

这是响应:

{
  "current_page": 1,
  "data": [{
    "id": 2,
    "object_id": 3,
    "booking_id": 1,
    "sender_id": 1,
    "receiver_id": 2,
    "message": "It is accepted",
    "type_id": 5,
    "read": 1,
    "archive": 0,
    "star": 0,
    "created_at": "2019-02-26 11:45:28",
    "updated_at": "2019-02-26 12:15:11",
    "created_time": "26\/02\/2019",
    "host_user": 0,
    "guest_user": 1,
    "sender": {
      "id": 1,
      "first_name": "Joe",
      "last_name": "Cos",
      "email": "email-91@hotmail.com",
      "profile_image": null,
      "balance": 0,
      "status": "Active",
      "created_at": "2019-02-21 15:19:26",
      "updated_at": "2019-02-21 15:19:26",
      "profile_src": "http:\/\/xxx.com\/public\/images\/user_pic-225x225.png"
    },
    "bookings": {
      "id": 1,
      "object_id": 3,
      "code": "mYuL4p",
      "host_id": 1,
      "user_id": 2,
      "start_date": "2019-02-26",
      "end_date": "2019-02-27",
      "status": "Accepted",
      "guest": 0,
      "total_night": 1,
      "per_night": 20,
      "base_price": 20,
      "cleaning_charge": 0,
      "guest_charge": 0,
      "service_charge": 0,
      "security_money": 0,
      "host_fee": 0,
      "total": 20,
      "booking_type": "request",
      "currency_code": "EUR",
      "cancellation": "Flexible",
      "transaction_id": "67427302T32774838",
      "payment_method_id": 1,
      "accepted_at": "2019-02-26 11:45:28",
      "expired_at": null,
      "declined_at": null,
      "cancelled_at": null,
      "cancelled_by": null,
      "created_at": "2019-02-26 11:37:36",
      "updated_at": "2019-02-26 11:45:28",
      "host_payout": 23,
      "label_color": "success",
      "date_range": "Feb 26 - 27, 2019",
      "expiration_time": "2019\/02\/27 11:37:36",
      "currency": {
        "id": 3,
        "name": "Europe",
        "code": "EUR",
        "symbol": "€",
        "rate": "0.88",
        "status": "Active",
        "default": "0",
        "org_symbol": "€"
      }
    },
    "object_address": {
      "id": 3,
      "object_id": 3,
      "address_line_1": "XXXXXXXXX, 4050-352 Porto, Portugal",
      "address_line_2": null,
      "latitude": "49.999",
      "longitude": "-8.88810419921",
      "city": "P",
      "state": "P",
      "country": "P",
      "postal_code": "4050-352"
    }
  }],
  "from": 1,
  "last_page": 1,
  "next_page_url": null,
  "per_page": 10,
  "prev_page_url": null,
  "to": 1,
  "total": 1
}

为什么我会收到这么多信息?如何控制这种疯狂?

1 个答案:

答案 0 :(得分:1)

Laravel可以“隐藏” JSON中的不同属性

https://laravel.com/docs/5.7/eloquent-serialization#hiding-attributes-from-json

在应用程序中的每个模型上,您可以指定$hidden属性以及要从JSON对象隐藏的属性数组。

因此,在您的示例中,您可以在模型中执行类似的操作

<?php
...
class Messages extends Model
{
    protected $hidden = [
        "object_id",
        "booking_id",
        "sender_id",
        "receiver_id",
        // add more columns you wish to hide
    ]; 
}

或者,还有$ visible属性以相反的方式工作,即,您仅定义应该在JSON对象中可见的字段

<?php
...
class Messages extends Model
{
    protected $visible = [
        "id",
        "name",
        // add more columns you wish to show
    ]; 
}