问题
I'm building a time tracker and i'm trying to fill up a table with the total time logged for each category type for each month of the year. I'm not sure what i'm doing wrong but my table isn't showing any data. I believe it has to do with the way i'm calling my rows, each number represents the category_id, ex:
<td>{{ $row[1] ?? '-' }}</td>
This is supposed to be using the total time from category_id 1 which is Overtime Hours.
Here's more info to make things more clear.
Controller:
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'));
}
If I dd($data), here's an example of 1 month.
"Oct" => Collection {#330 ▼
#items: array:1 [▼
0 => Collection {#333 ▼
#items: array:1 [▼
0 => "33:20:00"
]
}
]
}
This line should be showing 1 => "33:20:00" not 0=> "33:20:00", because it belongs to category_id = 1. So there must be a problem with my queries.
Table:
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Overtime Hours</th>
<th scope="col">Compensation Hours</th>
<th scope="col">Vacation</th>
<th scope="col">Personal Hours</th>
<th scope="col">Sick Hours</th>
</tr>
</thead>
<tbody>
@foreach($data as $month => $row)
<tr>
<th scope="row">{{ $month }}</th>
<td>{{ $row[1] ?? '-' }}</td>
<td>{{ $row[2] ?? '-' }}</td>
<td>{{ $row[3] ?? '-' }}</td>
<td>{{ $row[4] ?? '-' }}</td>
<td>{{ $row[5] ?? '-' }}</td>
</tr>
@endforeach
</tbody>
</table>
Migration for the form data:
public function up()
{
Schema::create('times', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->date('start_day');
$table->unsignedBigInteger('category_id');
$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');
});
}
Migration for categories:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('type');
});
}
Home Controller:
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
DB::table('categories')->insertOrIgnore([
['type' => 'OT Accrued'],
['type' => 'Comp Time Used'],
['type' => 'Vacation Time Used'],
['type' => 'Personal Hours Used'],
['type' => 'Sick Time']
]);
}
来源:https://stackoverflow.com/questions/58646159/laravel-table-not-showing-data