1

I used Postman to send request in my Laravel Api and I have a empty array to return. 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 ?

3
  • 1
    Show us how you're sending the request in Postman. $id is not part of $request->all() if you're looking for it in there. Commented Feb 7, 2020 at 15:51
  • What is sendResponse()? Please can you show the code for it. Commented Feb 7, 2020 at 15:52
  • do you send api_token in your request ? Commented Feb 7, 2020 at 16:02

1 Answer 1

7

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's it ! Thanks a lot... ;)
I know it's a pretty old question, but is this rule only for PATCH requests? Also, been stuck here for a whole day! Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.