#Getting 200 instead of 201 Response for Store

13 messages · Page 1 of 1 (latest)

plain pike
#

So I'm trying to pass a test given to me by the Sr dev. Any help is greatly appreciated. My Store should return a 201 response. I've gotten it to return a 200 response several different ways, but I'm obviously missing something. Here is my Store function in the controller:

` public function store(Request $request, GLCode $glcode)
{
$request->only([ 'id', 'location_id', 'gl_type_id', 'description', 'code', 'protected' ]);
$request->validate([
'id' => [ 'required', 'integer' ],
'location_id' => [ 'required', 'integer' ],
'gl_type_id' => [ 'required', 'integer' ],
'description' => [ 'required', 'string' ],
'code' => [ 'required', 'string' ],
'protected' => [ 'required', 'boolean' ],
]);

    $glcode->id = $request->get('id');
    $glcode->location_id = $request->get('location_id');
    $glcode->gl_type_id = $request->get('gl_type_id');
    $glcode->description = $request->get('description');
    $glcode->code = $request->get('code');
    $glcode->protected = $request->get('protected');

    return response()->json(new GLCodeResource($glcode));
}`
paper spoke
#

The last statement is simply return response()->json(), that's a 200 response. So it'll never randomly change that to a 201?!
I'd suggest to read the docs and use your IDE to see what parameters you can pass.

random pollen
#

There are several things wrong with your code.

paper spoke
random pollen
#

Firstly, you are calling $request->only() in open space, doing nothing with it. Then after you validate, you are attempting to set the properties on the GLCode model, yet you never insert or update any record at all, only "updating" the GLCode model instance in memory.

#

I would assume given you are using route model binding for GLCode, that you would be creating a new "related" model from the GLCode.

#

$glcode->redemptions()->create(...)

#

Assuming your GLCodeResource is one of laravels https://laravel.com/docs/9.x/eloquent-resources , then you do not even need to force a return type, just return the resource directly, and your clients should ensure they are using the Accept : application/json header.

return new GLCodeResource($glcode);
#

When you do this, then any model instance, whether returned directly, or through an eloquent api resource, will have an internal property set wasRecentlyCreated which informs laravels response factory to set the http response code to 201

#

Maybe I should be asking...what model are you actually trying to create? What does your route definition look like?

#

I have a feeling you type hinted GLCode $glcode in the controller method, but there is no glcodes/{glcode} endpoint for your store, thus you are actually just having the framework give you an empty instance of GLCode. If that is the case, you should not be type hinting that in the controller method, it is meant for route model binding.

#

And then you set the properties on the gl code instance, but you never save them