api route laravel 总是返回错误 400
Posted
技术标签:
【中文标题】api route laravel 总是返回错误 400【英文标题】:api route laravel always return error 400 【发布时间】:2021-12-18 14:43:21 【问题描述】:我有这样的路由 api.php
Route::group(['middleware' => ['jwt.verify']], function()
Route::get('logout', [ApiController::class, 'logout']);
Route::get('pengguna', [ApiController::class, 'getAuthenticatedUser']);
Route::get('get_kabupaten/provid', [KabupatenController::class, 'get_kabupaten']);
Route::get('get_kecamatan/kabid', [KecamatanController::class, 'get_kecamatan']);
Route::get('get_kelurahan/kecid', [DesaController::class, 'get_kelurahan']);
Route::get('get_bencana', [JenisbencanaController::class, 'get_bencana']);
Route::get('saport', [ReportController::class, 'get_laporan']);
Route::post('saport/create', [ReportController::class, 'store']);
Route::get('saport/id', [ReportController::class, 'show']);
// Route::get('laporan', [LaporanController::class, 'get_laporan']);
// Route::post('laporan/create', [LaporanController::class, 'store']);
// Route::get('laporan/id', [LaporanController::class, 'show']);
// Route::put('update/product', [LaporanController::class, 'update']);
// Route::delete('delete/product', [LaporanController::class, 'destroy']);
);
除了
,其他一切都正常 Route::get('pengguna', [ApiController::class, 'getAuthenticatedUser']);
Route::get('saport', [ReportController::class, 'get_laporan']);
Route::post('saport/create', [ReportController::class, 'store']);
Route::get('saport/id', [ReportController::class, 'show']);
这是我的报表控制器
<?php
namespace App\Http\Controllers;
use App\Models\Laporan;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Validator;
use Auth;
class ReportController extends Controller
public function get_laporan()
$laporan=Laporan::where('user_id', Auth::id())->get();;
return response()->json(compact('laporan'));
public function store(Request $request)
$data = $request->only( 'desa_id','bencana_id','koordinat','korban_kk','korban_orang','sebab_bencana');
$validator = Validator::make($data, [
'desa_id' => 'required',
'bencana_id' => 'required',
'koordinat' => 'required',
'korban_kk' => 'required',
'korban_orang' => 'required',
'sebab_bencana' => 'required',
]);
//Send failed response if request is not valid
if ($validator->fails())
return response()->json(['error' => $validator->messages()], 200);
$laporan = new Laporan;
$laporan->bencana_id = $request->bencana_id;
$laporan->desa_id= $request->desa_id;
$laporan->koordinat= $request->koordinat;
$laporan->user_id= Auth::id();
$laporan->korban_kk= $request->korban_kk;
$laporan->korban_orang= $request->korban_orang;
$laporan->sebab_bencana= $request->sebab_bencana;
$laporan->bantuan_diperlukan= $request->bantuan_diperlukan;
$laporan->respon_instansi= $request->respon_instansi;
$laporan->lokasi_pengungsian= $request->lokasi_pengungsian;
$laporan->pengungsi_kk= $request->pengungsi_kk;
$laporan->pengungsi_orang= $request->pengungsi_orang;
$laporan->permintaan_bantuan= $request->permintaan_bantuan;
$laporan->save();
return response()->json([
'success' => true,
'message' => 'laporan telah di input',
'data' => $laporan
], Response::HTTP_OK);
public function show($id)
$laporan=Laporan::where('user_id', Auth::id())->find($id);;
if (!$laporan)
return response()->json([
'success' => false,
'message' => 'Sorry, Laporan not found.'
], 400);
return response()->json(compact('laporan'));
我已将 int 从 laporan 重命名为向 saport 报告,创建新控制器,并使其 HTTPS 为 saport 始终返回 400。 据我所知,控制器的结构与其他控制器相同,因为我使用 meke:controller,以及为什么它返回 400 400 Bad Request。
我创建了新的基本路线,它返回 400
Route::get('saport', function ()
return 'Hello World';
);
缓存:清除
路线:清除
转储自动加载
尝试从 jwt 外部移除 saport,验证
全部返回 400
我都试过了。
顺便说一句,它在本地运行就像魅力一样,它只在共享主机中。
【问题讨论】:
【参考方案1】:我认为这是邮递员的问题,而不是 laravel。但如果有任何解决它的问题,请告诉我
【讨论】:
以上是关于api route laravel 总是返回错误 400的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 5.4 POST 到 API 重定向 302 而不是返回验证错误
Laravel 4 Auth::attempt() 总是返回 false
Laravel Dingo Api - 如何从 API 控制器返回 JSON 格式的错误响应?