首页 > 解决方案 > 使用选定的数据创建集合

问题描述

我有这个代码:

$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):

我怎样才能做到?

我需要用这种形式收集:'name'=> nazwa 1, [2020-12-22 , 2020-12-25]。我怎样才能做到这一点?

标签: phplaravel

解决方案


您可以尝试通过 Collection 方法进行操作

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

推荐阅读