Laravel:如何根据某些条件隐藏数据?
如果我有这个模型:Laravel:如何根据某些条件隐藏数据?,laravel,Laravel,如果我有这个模型: $result = [ { "date":"1/1/2021", "actions":[ { "id":1, "title:"test", "sub_actions":[ {
$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)
我不确定这是否是查询的输出,但对于一般情况,如果您有一个类似
[
{
"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();
或
我给您留下了使用数组而不是对象的示例
编辑:对嵌套属性进行筛选
让我们把json编码和解码放在一边一分钟。你有一系列的足球比赛,每一场比赛可以有零场或更多的比赛,而每一场比赛又可以有零场或更多的预测(预测?)
您希望只显示具有超过零个前缀的匹配项。这将删减没有上述属性的匹配,如果删减的匹配数组为空,那么也应该从最终结果中删减父fixture
该方法评估每项条件的真实性count($match['predect'])
为零,零为假值,与false
、NULL
、空字符串或空数组相同。非空数组本身可以满足truthyness测试,也可以满足count($array)
或count($array)>0的断言
检查时,您执行了以下等效操作(为了清晰起见,我不使用箭头函数):
$matches
是一个集合,它不是falsy,不管它没有项目。但是,如果使用,请改为:
$matches = collect($fixture['matches'])
->filter(function ($match) {
// their predect property is not empty
return count($match['predect']);
})->all();
而$matches
是一个空数组,它将“冒泡”有效地修剪父设备。如果任何夹具至少有一个匹配项通过了Truthynes测试,那么它将显示在最终结果中。它的匹配项
键不会被过滤,因为您不会根据嵌套的过滤器更改它们的内容
如果要重新声明每个fixture的匹配项以修剪没有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" => []
]
];
- 最后一个装置没有
匹配项
,因此它将被过滤掉
- 第二个装置有匹配项,但没有一个装置有
predect
,因此被过滤掉,而父装置没有匹配项
m,因此也将其过滤掉
- 第一个fixture具有匹配项,其中一个fixture没有
predect
,因此它被过滤掉。这使得父设备只剩下一个匹配项
最后一个数组是
$fixtures = [
[
"fixture_id" => 1,
"matches" => [
[
"match_id" => 1,
"home" => "Turkey",
"away" => "Italy",
"predect" => [
[
"h_predect" => 1,
"a_predect" => 1
]
]
]
]
]
];
请参阅此中的工作示例,我不确定这是否是查询的输出,但对于一般情况,如果您有一个类似
[
{
"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();
或
我给您留下了使用数组而不是对象的示例
编辑:对嵌套属性进行筛选
让我们把json编码和解码放在一边一分钟。你有一系列的足球比赛,每一场比赛可以有零场或更多的比赛,而每一场比赛又可以有零场或更多的预测(预测?)
您希望只显示具有超过零个前缀的匹配项。这将删减没有上述属性的匹配,如果删减的匹配数组为空,那么也应该从最终结果中删减父fixture
该方法评估每项条件的真实性count($match['predect'])
为零,零为假值,与false
、NULL
、空字符串或空数组相同。非空数组本身可以满足truthyness测试,也可以满足count($array)
或count($array)>0的断言
检查时,您执行了以下等效操作(为了清晰起见,我不使用箭头函数):
$matches
是一个集合,它不是falsy,不管它没有项目。但是,如果使用,请改为:
$matches = collect($fixture['matches'])
->filter(function ($match) {
// their predect property is not empty
return count($match['predect']);
})->all();
而$matches
是一个空数组,它将“冒泡”有效地修剪父设备。如果任何夹具至少有一个匹配项通过了Truthynes测试,那么它将显示在最终结果中。它的匹配项
键不会被过滤,因为您不会根据嵌套的过滤器更改它们的内容
如果要重新声明每个fixture的匹配项以修剪没有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" => []
]
];
- 最后一个装置没有
匹配项
,因此它将被过滤掉
- 第二个装置有匹配项,但没有一个装置有
predect
,因此被过滤掉,而父装置没有匹配项
m,因此也将其过滤掉
- 第一个fixture具有匹配项,其中一个fixture没有
predect
,因此它被过滤掉。这使得父设备只剩下一个匹配项
最后一个数组是
$fixtures = [
[
"fixture_id" => 1,
"matches" => [
[
"match_id" => 1,
"home" => "Turkey",
"away" => "Italy",
"predect" => [
[
"h_predect" => 1,
"a_predect" => 1
]
]
]
]
]
];
请参阅此中的工作示例非常感谢,我尝试了更复杂的json数据,如下所示:,我的json数据包含两个对象(日期和匹配项),匹配项有很多对象,其中一个是predect,我使用了它但不起作用:$results=collect($resultArray)->filter(fn($rows)=>collect($rows['matches'])->filter(fn($row)=>count row['predect'])->all()请看这个:我编辑了我的答案,给你留下了另一个例子:太谢谢你了,我没有