CRUD(L) by example
We have seen this controller before, but here are a few examples. The single most simple example of a RESTful call would be as shown in the following sections.
Create a GET
call to http://www.hotelwebsite.com/accommmodations/1
, where 1
would be the ID of the room:
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { return \MyCompany\Accommodation::findOrFail($id); }
This would return a single model as a JSON-encoded object:
{ "id": 1, "name": "Hotel On The Hill","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "location_id": 1, "created_at": "2015-02-08 20:13:10", "updated_at": "2015-02-08 20:13:10", "deleted_at": null }
crudL – list
Create a GET call to http://www.hotelwebsite.com/accommmodations
.
This is similar to the preceding code, yet slightly different:
/** Display a listing of the resource. * @return Response */ public function index() { return Accommodation::all(); }
This would return all of the models, automatically encoded as JSON objects; there is nothing else that is required. Formatting has been added so that the JSON results are more easily readable, but basically, the entire model is returned:
[{ "id": 1, "name": "Hotel On The Hill","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "location_id": 1, "created_at": "2015-02-08 20:13:10", "updated_at": "2015-02-08 20:13:10", "deleted_at": null } { "id": 2, "name": "Patterson Place", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "location_id": 2, "created_at": "2015-02-08 20:15:02", "updated_at": "2015-02-08 20:15:02", "deleted_at": null }, { "id": 3, "name": "Neat and Tidy Hotel", "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "location_id": 3, "created_at": "2015-02-08 20:17:34", "updated_at": "2015-02-08 20:17:34", "deleted_at": null } ]
Tip
The deleted_at
field is a soft delete or the recycle bin mechanism. It is either null
for not deleted or a date
/time
stamp for deleted.
Pagination
To add pagination, simply substitute all()
with paginate()
:
public function index() { return Accommodation::paginate(); }
The results will now look like this. The eloquent collection array is now moved inside a date
attribute:
{"total":15, "per_page":15, "current_page":1, "last_page":1, "next_page_url":null, "prev_page_url":null, "from":1, "to":15, "data":[{"id":9, "name":"Lovely Hotel", "description":"Lovely Hotel Greater Pittsburgh", ….
Crudl – create
Create a POST
call to .
To create a new model, a POST
call will be sent to /accommodations
. A JSON would be sent from the frontend as follows:
{ "name": "Lovely Hotel", "description": "Lovely Hotel Greater Pittsburgh", "location_id":1 }
The store
function might look something like this:
public function store() { $input = \Input::json(); $accommodation = new Accommodation; $accommodation->name = $input->get('name'); $accommodation->description = $input->get('description'); $accommodation->location_id = $input->get('location_id'); $accommodation->save(); return response($accommodation, 201) ; }
Tip
201
is the HTTP status code (HTTP/1.1 201 created
) for created
.
In this example, we returned the model as a JSON-encoded object. The object will include the ID that was inserted:
{ "name":"Lovely Hotel", "description":"Lovely Hotel Greater Pittsburgh", "location_id":1, "updated_at":"2015-03-13 20:48:19", "created_at":"2015-03-13 20:48:19", "id":26 }
crUdl – update
Create a PUT
call to , where
1
is the ID to be updated:
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { $input = \Input::json(); $accommodation = \MyCompany\Accommodation::findOrFail($id); $accommodation->name = $input->get('name'); $accommodation->description = $input->get('description'); $accommodation->location_id = $input->get('location_id'); $accommodation->save(); return response($accommodation, 200) ->header('Content-Type', 'application/json'); }
To update an existing model, the code is exactly the same as we used earlier, except that the following line is used to find the existing model:
$accommodation = Accommodation::find($id);
The PUT
verb would be sent to /accommodations/{id}
, where id
would be the numeric ID of the accommodations table.
cruDl – delete
To delete a model, create a DELETE
call tohttp://www.hotelwebsite.com/accommmodation/1
, where 1
is the ID to be deleted:
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $accommodation = Accommodation::find($id); $accommodation->delete(); return response('Deleted.', 200) ; }
Tip
There seems to be some disagreement about what the proper status code should be for a deleted model.