Lightweight jQuery plugin which allows you to communicate easily with your Laravel 5.4 server-side.
Install the plugin via npm install jquery-laravel-rest
. Then you just need to require('jquery-laravel-rest');
.
$.rest('update', my_data_object, success: (data) => void)
Consider there is no id specified explicitly - it will automatically be attached to the route. The plugin expects you to have the id specified in my_data_object
. The ID member can be specified as default value as we'll see later. Furthermore there is the base route required, i.e. when accessing a Laravel ResourceController for Users, then the url could be '~/users', for instance. The deeper parts (e.g. '/edit') will be added by the plugin.
The available REST-routes are: index
, create
, store
, show
, edit
, update
and destroy
. The request URLs are set up as decribed in the Laravel-Docs (table).
$.rest('default', { 'idField': 'my_id_member_on_each_object'});
The following default-values can be specified:
idField
: The field name which holds the ID of each object (defaults to 'id').url
: The default url (maybe helpful when having requests only belonging to a specific module/controller; defaults to '~/').csrf_token
: Set the CSRF token Laravel expects most likely from you. Might be helpful to specify this within a script-tag in your header:
<script> $.rest('csrf_token', {{ csrf_token(); }}); </script>
.
Alternatively you can configure the $.ajax with the csrf_token as X-CSRF-TOKEN as described here.before_request
: A callback function to call before each request. This callback takes no parameters and nothing is expected to be returned. It is not meant for preprocessing the data before request, but more for e.g. showing a loader.after_request
: Asbefore_request
, e.g. for hide the loader.
Hope this helps someone ;)