Php Laravel-表格未显示数据
我正在建立一个时间跟踪器,并试图填写一张表格,列出一年中每个月每个类别类型记录的总时间。我不确定我做错了什么,但我的表没有显示任何数据。我相信这与我调用行的方式有关,每个数字代表类别id,例如:Php Laravel-表格未显示数据,php,mysql,laravel,html-table,eloquent,Php,Mysql,Laravel,Html Table,Eloquent,我正在建立一个时间跟踪器,并试图填写一张表格,列出一年中每个月每个类别类型记录的总时间。我不确定我做错了什么,但我的表没有显示任何数据。我相信这与我调用行的方式有关,每个数字代表类别id,例如: <td>{{ $row[1] ?? '-' }}</td> 如果我使用dd($data),这里是一个1个月的示例 "Oct" => Collection {#330 ▼ #items: array:1 [▼ 0 => Collecti
<td>{{ $row[1] ?? '-' }}</td>
如果我使用dd($data),这里是一个1个月的示例
"Oct" => Collection {#330 ▼
#items: array:1 [▼
0 => Collection {#333 ▼
#items: array:1 [▼
0 => "33:20:00"
]
}
]
}
此行应显示1=>“33:20:00”而不是0=>“33:20:00”,因为它属于类别_id=1。所以我的查询肯定有问题。
表:
<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>
类别的迁移:
public function up()
{
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'));
}
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']
]);
}
事实上,我很有信心这不是我的错。您是否检查了表并验证了
category\u id
列在任何地方都不包含0
(通常数据库不会将0
指定为自动递增标识符,因此这必须是真的)。@Namoshek。不包含0。这是来自phpmyadmin的表的图像。我通过我的主页控制器插入这些类别,这样即使我运行php artisan migrate:fresh,它们也会一直存在。我将主控制器添加到我的操作系统中,这些都是类别本身,但是引用category\u id
列呢?@Namoshek。不,没有零。这是泰晤士报的截图我为每个类别添加了一次条目,如您所见。这是数据的dd:@Namoshek有什么想法吗?
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']
]);
}