【发布时间】:2021-09-09 19:08:36
【问题描述】:
我遇到了与 Laravel API 相关的问题。我也在使用 Vue CLI 3。当我与php artisan r:l 核对时,两条路线都已注册。 search/{keyword} 路由没有问题。问题是download-pdf 的路由,它无法显示响应,并且当我在 Postman 中测试它时得到一个空白响应。我错过了什么吗?
这是路线:
Route::group(['prefix' => 'staff', 'middleware' => ['auth:staff-api', 'scopes:staff']], function () {
Route::get('staff-management/search/{keyword}', [StaffController::class, 'searchStaff']);
Route::get('staff-management/download-pdf', [StaffController::class, 'downloadPDF']);
});
这是控制器:
public function searchStaff($keyword) {
$staff = Staff::where('name', 'LIKE', "%{$keyword}%")
->orWhere('email', 'LIKE', "%{$keyword}%")
->get();
return response()->json([
'staff' => $staff
], Response::HTTP_OK);
}
public function downloadPDF() {
$staffs = Staff::all();
return response()->json([
'success' => true,
'message' => 'Previewed',
'pdf' => $staffs,
]);
}
[更新] 当我更改为下面的代码时,它正在工作。不知道。。真的很奇怪。
路线
Route::group(['prefix' => 'staff', 'middleware' => ['auth:staff-api', 'scopes:staff']], function () {
Route::get('staff-management/download-pdf/{keyword}', [StaffController::class, 'downloadPDF']);
});
控制器
public function downloadPDF($keyword)
{
$staff = Staff::where('name', 'LIKE', "%{$keyword}%")
->orWhere('email', 'LIKE', "%{$keyword}%")
->get();
return response()->json([
'staff' => $staff
], Response::HTTP_OK);
}
【问题讨论】:
-
请向我们展示您的邮递员回复和标题回复
-
我已经更新了问题。请检查一下!
-
仅供参考,我使用的是 Vue CLI 3
-
Laravel 日志文件显示什么?存储/日志/laravel.log ? $staffs = Staff::all(); 是什么意思?评价为。它有助于在开发过程中使用 Log::info 记录内容。
-
@SeadLab,你的东西正在工作,看看你只看到 1 行但白色的响应图像......切换到 RAW 而不是 PRETTY
标签: laravel