假设它是一个集合,您可以只使用collapse() 方法。
$h = $request->get('holiday');
/*
Illuminate\Support\Collection {
all: [
Utility {
+"from": "2021/05/10",
+"to": "2021/05/14",
},
Utility {
+"from": "2021/05/17",
+"to": "2021/05/20",
},
],
}
*/
$h->map(function ($utility) { return [$utility->from, $utility->to]; });
/*
Illuminate\Support\Collection {
all: [
[
"2021/05/10",
"2021/05/14",
],
[
"2021/05/17",
"2021/05/20",
],
],
}
*/
$h->map(function ($utility) { return [$utility->from, $utility->to]; })
->collapse();
/*
Illuminate\Support\Collection {
all: [
"2021/05/10",
"2021/05/14",
"2021/05/17",
"2021/05/20",
],
}
*/
$h->map(function ($utility) { return [$utility->from, $utility->to]; })
->collapse()
->all();
/*
[
"2021/05/10",
"2021/05/14",
"2021/05/17",
"2021/05/20",
],
*/
如果$h = $request->get('holiday'); 不是一个集合,而是一个像下面这样的 json 字符串,
[{"from":"2021/05/10","to":"2021/05/14"},{"from":"2021/05/17","to":"2021/05/20"}]
您可以使用 collect(json_decode($h)) 将其转换为 Collection 并执行相同操作
->map(...)->collapse()->all() 序列。
如果您想在刀片文件中回显这些值(带有编号),您可以这样做:
@foreach ($h as $index => $value)
{{ $index + 1 }} {{ $value }}<br>
@endforeach