Adding POST, DELETE, PUT

To complete the basic amount of functionality in line with basic CRUD functionality we need to be able to add a new member to the tennisclub, to update an existing members details and to delete members. In adding this functionality we will use the POST, PUT and DELETE verbs which are described in the HTTP specification.

While the code to complete these actions will be part of the MemberController it is the router which allows us to use specific HTTP methods.

First lets add the create function. Add the following code to app\Http\MemberController.php

public function create(Request $request)
{
    $arr = $request->json()->all();
    $member = new Member();
    $member->forceFill($arr);
    $member->save();
    return response()->json($member, 201);
}

It should be pointed out that this is a bit of a rough and ready create action. There is no error checking. At this point we just want to see if we can POST some json and get it to create a new member in the tennisclub database. Later we may add checks and correct HTTP error messages. Next add a corresponding route inside API group in routes\web.php.

$router->post('members', ['uses' => 'MemberController@create']);

Note the POST method highlighted in bold. Next add an update function to the \app\Http\MemberController.php file

public function update($id, Request $request)
{
    $Member = Member::findOrFail($id);
    $Member->update($request->all());
    return response()->json($Member, 200);
}

add a corresponding route to the routes\web.php file

$router->put('members/{id}', ['uses' => 'MemberController@update']);

Note the PUT method highlighted in bold. Finally add the delete function to the \app\Http\MemberController.php file

public function delete($id)
{
    Member::findOrFail($id)->delete();
    return response('Deleted Successfully', 200);
}

and the corresponding route the routes\web.php file. Note the DELETE method highlighted in bold.

$router->delete('members/{id}', ['uses' => 'MemberController@delete']);

Now we are ready to test the methods using curl. To test the POST method we first need to create a file with some json data relating to the new member. Save the following json to a file called newMember.json in the folder you're working in.

{"firstname":"Joseph","surname":"bloggs","dateofbirth":"2002-01-01","membertype":"Senior"}

Now execute the following command on the command line to POST this json data to your endpoint

curl -d @newMember.json -H "Content-Type: application/json" -X POST http://localhost:8000/api/members

This will result in a new member being added to your database. Now let's test the PUT method. Save the following json to a file called changeMember.json in the folder you're working in.

{"firstname":"Jedd"}

Now execute the PUT request

curl -d @changeMember.json -H "Content-Type: application/json" -X PUT http://localhost:8000/api/members/2

The delete request doesn't require a separate json file. To delete the member with id=5 call the following DELETE method command on the members resource

curl -X DELETE http://localhost:8000/api/members/5

Now we have the full suite of HTTP methods GET, POST, PUT and DELETE working on the api/members resource.

Leave a Reply