John Davidson

php - Create collection with selected data

0 comments
Message:


I have this code:


$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);
}

It's return me:


 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
}
]
}

It's work fine.


I need show in my blade something like this (by course_id):



  • nazwa 1 (short_name) has terms: 2020-12-22 , 2020-12-25 (starting_at)



  • nazwa 2 (short_name) has terms: 2020-12-27 (starting_at)




How can I make it?


I need collection with this form: 'name'=> nazwa 1, [2020-12-22 , 2020-12-25].
How can I make this?



My Answer:
You can achieve this by iterating through the $terms collection and creating a new collection with the desired format. Here's how you can do it:

php
$termsArray = [];

foreach ($terms as $term) {
$name = $term->short_name;
$startingAt = $term->starting_at;

if (!isset($termsArray[$name])) {
$termsArray[$name] = [];
}

$termsArray[$name][] = $startingAt;
}

// Now $termsArray will have the desired format
foreach ($termsArray as $name => $startingAts) {
echo $name . ' (short_name) has terms: ' . implode(', ', $startingAts) . ' (starting_at)' . '
';
}


This code will create a new collection $termsArray with the format 'name' => [starting_at1, starting_at2, ...]. Then, you can iterate through this collection to display the desired output in your blade file.

Rate this post

4 of 5 based on 4332 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us