Essential CRUD

For simplicity, I have numbered each of the rows. The first and second items represent the read part of CRUD.

The first item, which is a GET call to the plural form of the model name, is rather simple; it displays all of the items. Sometimes, this is called a list to differentiate it from the read of a single record. Adding alist would thus expand the acronym to CRUDL. They could be paginated or require authorization.

The second item, also a GET call, adds the ID of the model to the end of the URL, displaying a single model with that corresponding ID. This could also require authentication but not paging.

The third item represents the create part of CRUD. It uses the POST verb to create a new model. Note that the URL format is the same as the first item; this demonstrates the importance of the verb to distinguish between the actions.

The fourth, fifth, and sixth items use the new HTTP verbs that were not supported by all browsers. Whether or not the verbs are supported, JavaScript libraries and frameworks, such as jQuery, will send the verb in a way that Laravel can properly handle.

The fourth item is the update part of CRUD and updates the model using the PUT verb. Note that it has the same URL format as the second, as it needs to know which model to update. It is also idempotent, which means that the entire model must be updated.

The fifth item is similar to the fourth item; it updates the model, but uses the PATCH verb. This is used to indicate that the model will be partially modified, which means that one or more of the model’s attributes have to be changed.

The sixth item deletes a single model and thus requires the model’s ID, using the self-explanatoryDELETE verb.

Bonus features

Laravel adds two additional methods that are not usually part of a standard RESTful API. A GETmethod on the model URL, adding create is used to display a form to create the model. A GETmethod on the model URL with its ID, adding edit is used to display a form to create the model. These two functions are useful for providing a URL that will load a form, even though this type of usage is not a standard RESTful:

HTTP VERBFunctionURL
GETThis displays an accommodation creation form/accommodations/create
GETThis displays an accommodation modification/update form/accommodations/{id}/edit

Controller creation

To create a controller for the accommodations, the following Artisan command is used:

$ php artisan make:controller AccommodationsController

    <?php namespace MyCompany\Http\Controllers;
    
    use MyCompany\Http\Requests;
    use MyCompany\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    
    class AccommodationController extends Controller {
    
        /**
        * Display a listing of the resource.
        * @return Response
        */
        public function index()
        {
        }
        
        /**
        * Show the form for creating a new resource.
        * @return Response
        */
        public function create()
        {
        }
        
        /**
        * Store a newly created resource in storage.
        * @return Response
        */
        public function store()
        {
        }
    
        /**
        * Display the specified resource.
        * @param  int  $id
        * @return Response
        */
        public function show($id)
        {
        }
    
        /**
        * Show the form for editing the specified resource.
        * @param  int  $id
        * @return Response
        */
        public function edit($id)
        {
        }
    
        /**
        * Update the specified resource in storage.
        *
        * @param  int  $id
        * @return Response
        */
        public function update($id)
        {
        }
    
        /**
        * Remove the specified resource from storage.
        * @param  int  $id
        * @return Response
        */
        public function destroy($id)
        {
        }
    }