JSON返回过多的信息

问题描述 投票:0回答:1

我正在使用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 protected]",
      "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
}

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

php json database laravel
1个回答
1
投票

Laravel有一种方法可以“隐藏”JSON的不同属性

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

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

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

<?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
    ]; 
}

© www.soinside.com 2019 - 2024. All rights reserved.