现在,我将时间条目按类别和月份分组。类别存储在单独的表中,我在主时间表上使用称为category_id的外键约束来引用它们。我将时间按category_id分组,但我想按类别名称(即字符串值)对时间进行分组。
是否应该通过为类别创建列来将类别存储在主时间表中?
当前,我有一个带有以下内容的“时间”表:
Schema::create('times', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->date('start_day');
$table->time('start_time');
$table->time('finish_time');
$table->time('duration');
$table->text('notes');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
}
和带有以下内容的类别表:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('type');
});
}
我将时间按类别和月份分组,如下所示:
public function show(User $user)
{
$data = collect(array_fill(1, 12, []))
->replace(
$user->times()
->whereYear('start_day', 2019)
->groupBy('category_id', \DB::raw('month(start_day)'))
->select([
'category_id',
\DB::raw('month(start_day) as month'),
\DB::raw('sum(duration) as duration'),
])
->orderBy(\DB::raw('month(start_day)'))
->get()
->mapToGroups(function ($item) {
return [$item['month'] => [
$item['category_id'] => $this->formatDuration($item['duration'])
]];
})
->mapWithKeys(function ($item, $key) {
return [$key => $item->collapse()];
})
)
->mapToGroups(function ($item, $key) {
$month = \DateTime::createFromFormat('!m', $key)->format('M');
return [$month => $item];
});
return view('report.show', compact('data'));
}
如果我dd($ data)会输出,其中包含11月每个类别的1次输入示例。我一共有5个类别:
Collection {#327 ▼
#items: array:12 [▼
"Jan" => Collection {#315 ▶}
"Feb" => Collection {#332 ▶}
"Mar" => Collection {#335 ▶}
"Apr" => Collection {#334 ▶}
"May" => Collection {#333 ▶}
"Jun" => Collection {#316 ▶}
"Jul" => Collection {#320 ▶}
"Aug" => Collection {#313 ▶}
"Sep" => Collection {#318 ▶}
"Oct" => Collection {#294 ▶}
"Nov" => Collection {#321 ▼
#items: array:1 [▼
0 => Collection {#336 ▼
#items: array:5 [▼
0 => "12:00"
1 => "11:00"
2 => "12:00"
3 => "12:00"
4 => "12:00"
]
}
]
}
"Dec" => Collection {#328 ▶}
]
相反,我想要的输出是:
"Nov" => Collection {#321 ▼
#items: array:1 [▼
0 => Collection {#336 ▼
#items: array:5 [▼
'Category Type 1' => "12:00"
'Category Type 2' => "11:00"
'Category Type 3' => "12:00"
'Category Type 4' => "12:00"
'Category Type 5' => "12:00"
]
}
是否可以通过使用category_id从类别表中引用“类型”?
时间模型:
class Time extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->hasOne('App\Category');
}
}
类别模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Category extends Model
{
protected $guarded = [];
public function time() {
return $this->belongsTo('App\Time');
}
}
用户模型:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function times()
{
return $this->hasMany(Time::class);
}
public function categories()
{
return $this->hasMany('App\Category');
}
}