[英]Laravel 6.2 : $request->all() return a empty array
I used Postman to send request in my Laravel Api and I have a empty array to return.我使用 Postman 在我的 Laravel Api 中发送请求,我有一个空数组要返回。 And I don't know why ?
我不知道为什么?
My Route :我的路线:
Route::middleware('auth:api')->group( function () {
Route::resource('reservations', 'ReservationController');
});
My reservationController :我的预订控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ReservationCreateRequest;
use App\Repositories\ReservationRepository;
use App\Http\Resources\Reservation as ReservationResource;
class ReservationController extends BaseController
{
protected $entrepriseRepository;
public function __construct(ReservationRepository $reservationRepository)
{
$this->reservationRepository = $reservationRepository;
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$reservation = $this->reservationRepository->getById($id);
return $this->sendResponse(new ReservationResource($reservation), 'Reservation');
}
/**
* Update the specified resource in storage.
*
* @param Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$inputs = $request->all();
if (!$reservation = $this->reservationRepository->update($id, $inputs))
{
return $this->sendError('Impossible de mettre à jour', $request->errors(), 400);
}
return $this->sendResponse(new ReservationResource($reservation), 'Reservation mise à jour avec succès !' , 200);
}
}
if I return just a response with如果我只返回一个响应
return response($request->all());
I have a empty array...我有一个空数组...
The method方法
show($id)
works correctly...工作正常...
Any suggestions someone ?有人有什么建议吗?
If you're sending PATCH
request from postman, you need to send it with x-www-form-urlencoded
as Laravel unfortunately gives empty request for form-data
with PATCH
request.如果您要从邮递员发送
PATCH
请求,则需要使用x-www-form-urlencoded
发送它,因为不幸的是,Laravel 会使用PATCH
请求为form-data
提供空请求。
As a side note, you can't send files with x-www-form-urlencoded
so if you have files in your request, you should send a POST
request using form-data
and _method: PATCH
in the request body, Laravel will automatically treat it like a PATCH
request.作为旁注,您不能使用
x-www-form-urlencoded
发送文件,因此如果您的请求中有文件,您应该在请求正文中使用form-data
和_method: PATCH
发送POST
请求,Laravel 将自动将其视为PATCH
请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.