php - 使用选定的数据创建集合
问题描述
我有这个代码:
$terms = $this->model
->where('course_terms.is_active', true)
->where(function ($query) use ($dateFrom, $dateTo) {
if ($dateFrom != "" && $dateTo == "") {
$query->whereDate('starting_at', '>=', $dateFrom);
} elseif ($dateFrom == "" && $dateTo != "") {
$query->whereDate('starting_at', '<=', $dateTo);
} else {
$query->whereBetween('starting_at', [$dateFrom, $dateTo]);
}
})
->select(['courses.short_name AS short_name', 'course_terms.*'])
->leftJoin('courses', 'courses.id', '=', 'course_terms.course_id')
->orderBy('starting_at', 'asc')
->get();
$termsArray = [];
foreach ($terms as $term)
{
dump($term);
}
它还给我:
Illuminate\Database\Eloquent\Collection {#1814 ▼
#items: array:3 [▼
0 => App\Models\CourseTerm {#1815 ▼
#fillable: array:8 [▶]
#dates: array:5 [▶]
#casts: array:6 [▶]
#appends: array:1 [▶]
#connection: "mysql"
#table: "course_terms"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [▼
"short_name" => "nazwa 1"
"id" => 2
"starting_at" => "2020-12-22"
"days" => 123
"course_id" => 1
"max_participants" => 12312
"additional_notes" => "Fajny kurs 1213"
"is_active" => 1
"is_confirmed" => 1
"created_at" => "2020-12-14 12:24:37"
"updated_at" => "2020-12-14 12:39:22"
"deleted_at" => null
"created_by" => 1
"updated_by" => 1
"deleted_by" => null
"diary_generated_at" => "2020-12-14 13:40:40"
]
#original: array:16 [▶]
#changes: []
#classCastCache: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
1 => App\Models\CourseTerm {#1816 ▼
#fillable: array:8 [▶]
#dates: array:5 [▶]
#casts: array:6 [▶]
#appends: array:1 [▶]
#connection: "mysql"
#table: "course_terms"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [▼
"short_name" => "nazwa 1"
"id" => 1
"starting_at" => "2020-12-25"
"days" => 12
"course_id" => 1
"max_participants" => 12
"additional_notes" => "Fajny kurs"
"is_active" => 1
"is_confirmed" => 1
"created_at" => "2020-12-14 11:34:30"
"updated_at" => "2020-12-14 12:39:26"
"deleted_at" => null
"created_by" => 1
"updated_by" => 1
"deleted_by" => null
"diary_generated_at" => "2020-12-14 12:39:26"
]
#original: array:16 [▶]
#changes: []
#classCastCache: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
2 => App\Models\CourseTerm {#1817 ▼
#fillable: array:8 [▶]
#dates: array:5 [▶]
#casts: array:6 [▶]
#appends: array:1 [▶]
#connection: "mysql"
#table: "course_terms"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [▼
"short_name" => "nazwa 2"
"id" => 3
"starting_at" => "2020-12-27"
"days" => 12
"course_id" => 2
"max_participants" => 12
"additional_notes" => "kurs na 100"
"is_active" => 1
"is_confirmed" => 1
"created_at" => "2020-12-14 15:31:44"
"updated_at" => null
"deleted_at" => null
"created_by" => 1
"updated_by" => 1
"deleted_by" => null
"diary_generated_at" => "2020-12-14 14:32:01"
]
#original: array:16 [▶]
#changes: []
#classCastCache: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
]
}
它工作正常。
我需要在我的刀片中显示类似这样的内容(通过 course_id):
nazwa 1 (short_name) 的条款: 2020-12-22 , 2020-12-25 (starting_at)
nazwa 2 (short_name) 的条款:2020-12-27 (starting_at)
我怎样才能做到?
我需要用这种形式收集:'name'=> nazwa 1, [2020-12-22 , 2020-12-25]。我怎样才能做到这一点?
解决方案
您可以尝试通过 Collection 方法进行操作
$terms->groupBy('short_name')
->map(function($records){
return $records->map(function($term){
return $term->starting_at;
})->all();
})->all();
推荐阅读
- laravel - 我的查询不起作用
- android - 存根索引指向没有 PSI 的文件:com.intellij.openapi.fileTypes.UnknownFileType@23713b43
- javascript - 使用 jquery 从 html 克隆中删除元素标记时出现错误
- sql-server - 使用 REPLACE 函数创建 SQL Server 函数
- javascript - 嵌入一段html和javascript
- data-synchronization - 是否有任何文件属性表明特定文件已与云同步
- java - 杰克逊解析课不起作用
- c - (*prt)[N][N] 在处理堆分配内存时如何工作?
- ormlite-servicestack - ServiceStack OrmLite - 对 > 和 < 表达式使用字符串类型
- javascript - JavaScript 执行器在谷歌浏览器中工作,但在 Firefox 中不工作