php - Create collection with selected data

Solution:

You can try manipulating via Collection methods

$terms->groupBy('short_name')  
  ->map(function($records){
    return $records->map(function($term){
        return $term->starting_at;
    })->all();
  })->all();

Answer

Solution:

Try this one

$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')
        ->groupBy('created_by') // or your common column by which you identify person ('nazwa 1' as per example)
        ->get();

So it groups 2 array in one based on groupby key. And show data according that.

Source