如何解决Laravel:如何根据某些条件隐藏数据? 编辑:过滤嵌套属性
如果我有这个模型:
$result =
[
{
"date":"1/1/2021","actions":[
{
"id":1,"title:"test","sub_actions":[
{
"id":1
}
{
"id":2
}
]
}
],"date":"2/1/2021","actions":[
{
"id":2,"title:"test2","sub_actions":[
{
"id":1
}
{
"id":2
}
]
}
]
}
]
我想如果在这些日期中的某些日期中,由于某种原因,操作计数为零,以便不在列表中同时显示(日期和操作)? 像这样:
if($result->actions->count() == 0)
解决方法
我不确定这是否是查询的输出,但对于一般情况,如果您有一个看起来像的 json
[
{
"date": "1/1/2021","actions": [
{
"id": 1,"title": "test","sub_actions": [
{
"id": 1
},{
"id": 2
}
]
}
]
},{
"date": "2/1/2021","actions": [
]
}
]
然后你解码成这样的变量
$resultArray = json_decode(
'[{"date":"1/1/2021","actions":[{"id":1,"title":"test","sub_actions":[{"id":1},{"id":2}]}]},{"date":"2/1/2021","actions":[]}]'
);
你可以把它变成一个集合,然后在上面应用 filter
$recordsWithActions = collect($resultArray)->filter(fn($row)=> count($row->actions))->all();
或
$recordsWithActions = collect($resultArray)->filter(
function($row) {
// it would be $row['actions'] if you did json_decode(...,true)
return count($row->actions)>0;
}
)->all();
我在 Laravel Playground 处留下了使用数组而不是对象的示例
编辑:过滤嵌套属性
让我们暂时将json编码和解码放在一边。您有一系列足球赛程,每场比赛可以有零个或多个比赛,而每场比赛又可以有零个或多个预测(预测?)。
您只想显示超过零个预测的匹配项。这将修剪没有所述属性的匹配,如果修剪的匹配数组为空,则父夹具也应从最终结果中修剪。
Collecttion::filter 方法评估每个项目的条件的真实性。 count($match['predect'])
为零,零为假值,与 false
、NULL
相同,为空字符串或空数组。非空数组本身将满足真实性测试,或者满足 count($array)
或 count($array)>0
的断言。
检查 your example,你做了相当于(为了清楚起见,我不会使用箭头函数):
// show me the items for which the next condition is truthy
$myresults = collect($fixtures)->filter(function ($fixture) {
// matches is a collection of items for which is true that
$matches = collect($fixture['matches'])
->filter(function ($match) {
// their predect property is not empty
return count($match['predect']);
});
// this is the truthyness test to check
return $matches;
})->all();
$matches
是一个集合,不管它没有项都不是假的。但是,如果您使用,则改为:
$matches = collect($fixture['matches'])
->filter(function ($match) {
// their predect property is not empty
return count($match['predect']);
})->all();
和 $matches
是一个空数组,它会“冒泡”有效地修剪父设备。如果任何装置至少有一个匹配通过真实性测试,那么它将显示在最终结果中。它的 matches
键不会被过滤,因为您不会在嵌套过滤器的基础上更改它们的内容。
如果您想重新声明每个装置的匹配项以修剪没有 predect
的那些,您可以像这样使用 Collection::map
方法:
$fixtures_with_passing_matches = collect($fixtures)
->map(function($fixture) {
$fixture['matches'] = collect($fixture['matches'])
->filter(function ($match) {
return count($match['predect']);
})->all();
return $fixture;
})->all();
您可以在映射之后链接过滤器方法,从而修剪在重新声明匹配项后没有留下任何设备的设备
$fixtures_with_passing_matches = collect($fixtures)
->map(function($fixture) {
$fixture['matches'] = collect($fixture['matches'])
->filter(function ($match) {
return count($match['predect']);
})->all();
return $fixture;
})->filter(function($fixture) {
return count($fixture['matches'])>0;
})->all();
那么让我们看看下面的示例数组:
$fixtures = [
[
"fixture_id" => 1,"matches" => [
[
"match_id" => 1,"home" => "Turkey","away" => "Italy","predect" => [
[
"h_predect" => 1,"a_predect" => 1
]
]
],[
"match_id" => 3,"home" => "Denmark","away" => "Finland","predect" => []
]
]
],[
"fixture_id" => 2,"matches" => [
[
"match_id" => 4,"home" => "France","away" => "Norway","predect" => []
]
]
],[
"fixture_id" => 3,"matches" => []
]
];
- 最后一个fixture没有
matches
,所以会被过滤掉 - 第二个设备有匹配项,但它们都没有
predect
,因此被过滤掉,留下没有matches
m 的父设备,因此也将其过滤掉 - 第一个装置有匹配项,其中一个没有
predect
,因此被过滤掉。这让父装置只有一场比赛
最后的数组是
$fixtures = [
[
"fixture_id" => 1,"a_predect" => 1
]
]
]
]
]
];
请参阅此 Laravel Playground
中的工作示例版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。