简体   繁体   English

Laravel 6.2:$request->all() 返回一个空数组

[英]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.

相关问题 $request-&gt;json()-&gt;all() 返回空 Laravel - $request->json()->all() return empty Laravel Laravel:$ request-&gt; all()使用json xhr调用时给出空数组 - Laravel: $request->all() gives empty array when called with json xhr Laravel $请求-&gt;expectsJson() - Laravel $request->expectsJson() Laravel:当我使用$ request-&gt; all()但在fill($ request-&gt; only(&#39;...&#39;)中工作时,fill()和update()不起作用 - Laravel : fill() and update() not work when i use $request->all() but work in fill($request->only('…')) Symfony4 (API) - 尝试使用 request-&gt;files-&gt;all() 上传文件给我一个空的结果 - Symfony4 (API) - Trying to upload a file using request->files->all() gives me an empty result 在cakephp 3.5中发布json数组时,$ this-&gt; request-&gt; getData()返回不合适的响应 - $this->request->getData() return inappropriate response in case of post json array in cakephp 3.5 如何使用具有Eloquent模型的Request-&gt; all() - How to use Request->all() with Eloquent models 无法使用 $request-&gt;ajax() 和 Laravel 5.4 检查请求是否为 ajax - Can't check if request is ajax with $request->ajax() and Laravel 5.4 请求addparameter数组,但返回空 - request addparameter array but return empty in response JSON请求出现404错误-传递了所有标头,但没有运气 - 404 error at the json request- Passed all the headers possible but no luck
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM