我正在尝试在Laravel5模型中实现日志过滤器。存储在mongodb集合中的日志。
记录具有这种结构。但这种结构并不固定!它可能只是一个没有所有这些属性的对象。 此外它可能非常不同
所以这是可能的:
{
"_id": ObjectId("5950cacd64b6bd1324005095"),
"request": "44b2fd871f989bb5a1416cbee2dbae5f",
"type": "calculate",
"data": {
"request": {
"request_uri": "http://api.server/method/packet/260c013a7b791b93997dd7b9abf15f68/go",
"region_id": "249",
"country_id": "",
"count_insureds": "1",
"citizenship_id": "198",
"period": "18",
"date_from": "2017-06-30",
"date_till": "2017-07-17",
"last_name": "Asdasd",
"first_name": "Sasd",
"birthday": "1978-06-08",
"passport": "WE211",
"email": "",
"address": "",
"identific": "",
"persons": {
"1": {
"last_name": "",
"first_name": "",
"birthday": "",
"passport": "",
}
},
"igree_1": "1",
"igree_2": "1",
"gateway": "mygateway",
"packet_id": "99",
"_token": "5288e7875e97d165443fba0e6b5577ff"
},
"response": {
"hash": "44Qwf5T6znXx2",
"sn": "КМ5000073",
"total_price": "899.87000000",
"total_price_formatted": "899.87",
"currency": "EUR",
"total_base_price": 14.58,
"rate": "29.0445"
}
},
"updated_at": ISODate("2017-06-26T08:50:21.0Z"),
"created_at": ISODate("2017-06-26T08:50:21.0Z")
}
这是另一个:
{
"_id": ObjectId("5950cac964b6bd1324005093"),
"request": "094932d4f83069b9e0ac759277a557b0",
"type": "draft",
"data": {
"BILL_AMOUNT": "899.87",
"SHOPORDERNUMBER": "357397a1fdf22a4daac6c955e1bd71-S9F8wGTjBRzGAVYw-1498466995",
"APPROVALCODE": "TESTPM",
"RECEIPT_URL": "https://www.website.com/services/receipts/get-receipts/shop-bill-id/35343744448e8c7f62e266e9ddb9345785357cf7b526bdca57e971720a7dfff4c5850f2e24a [...]",
"TOKEN": "18323839313034333331160415C2A559EBAF18E61D2739BBEF7AB5681A10B15A7CB29E6ACC12EACAC46C0C0F297EEC04918B4811E223656B776214E5B2EC5818E7394698D5BF07FB17699E [...]",
"CARD_PAYMENT_SYSTEM": "VISA",
"CARD_LAST_DIGITS": "1111",
"RESULT": "0"
},
"updated_at": ISODate("2017-06-26T08:50:17.947Z"),
"created_at": ISODate("2017-06-26T08:50:17.947Z")
}
我已经编写了用于过滤数据的范围方法。它看起来像这样:
public function scopeFilter($query, $filters)
{
if ($requestType = array_get($filters, 'request_type')) {
$query->where('type', '=', $requestType);
}
if ($data = array_get($filters, 'data')) {
//$query->where('data', 'like', '%' . trim($data) . '%');
$query->raw(function ($collection) use ($data) {
return $collection->find([
'data' => [
'$in' => [$data]
]
]);
});
}
// ...
}
问题是:我无法过滤
data
,因为它有不同的结构。正如您所看到的,我尝试使用LIKE
语句进行搜索 但它没用。使用$in
过滤会返回所有记录。
任何建议都将受到赞赏。
提前致谢。