php - JSON 返回过多信息
问题描述
我正在使用 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
}
为什么我会得到这么多信息??如何控制这种疯狂?
解决方案
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
];
}
推荐阅读
- jsonata - 如何使用 JSONata 更改某些属性而不描述所有未更改的属性?
- oracle - TNS 12545, 00515 - 没有运行 tnslsnr
- python - 如何在 setup.py 中包含单个 .py 文件?
- azure - 如何在 Azure 中创建服务原则密钥
- laravel - 过滤嵌套关系表 Laravel 8
- sql - 从 2 个表中删除条目
- roblox - Roblox perlin 噪音孔
- reactjs - 安装 npm 以使用节点创建反应应用程序
- javascript - 将数据从一张表传输到另一张表并删除谷歌表中的旧数据
- python - 在 Python 中将 SQL 结果转换为字符串格式