3

Laravel 5 uses put/patch verbs to update a resource while Angular ng.resource uses post by default for both create and update. How to globally set Laravel's Route::resource to follow the Angular behavior (a Laravel route for each ng resource)?

(It's also possible to make Angular compatible to Laravel, but I am not sure which approach is better.)

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

2 Answers2

3

I don't know laravel's REST capabilities. But still i would suggest to modify Angular's behaviour.

PUT

Implementing PUT its quite easy.

You can modify behaviour of ng-resource while you are creating factory with $resource(url, parameters, actions), third parameter describes custom actions ...

In https://docs.angularjs.org/api/ngResource/service/$resource there is example about creating PUT method which will be available as update on service and $update on instance. :

app.factory('Notes', function($resource) {
return $resource('/notes/:id', null,
    {
        'update': { method:'PUT' }
    });
}]);
// In our controller we get the ID from the URL using ngRoute and $routeParams
// We pass in $routeParams and our Notes factory along with $scope
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
                                   function($scope, $routeParams, Notes) {
// First get a note object from the factory
var note = Notes.get({ id:$routeParams.id });
$id = note.id;

// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id }, note);
// This will PUT /notes/ID with the note object in the request payload
});

PATCH

Creating PATCH behaviour is theoretically also possible, it is described here - Partial Updates (aka PATCH) using a $resource based service?

But I wouldn't do that with ng-resource. You can do many things by defining transformRequest or transformResponse function (i use this for utilizing Java Spring REST API). But still ng-resource doesn't support PATCH on its own so If you need I'd rather try different layer for REST.

Community
  • 1
  • 1
-1

That's wrong, you can use PUT/PATCH with angularjs too, please read about it at $http reference page

Shortcut methods

Shortcut methods are also available. All shortcut methods require passing in the URL, and request data must be passed in for POST/PUT requests.

$http.get('/someUrl').then(successCallback);
$http.post('/someUrl', data).then(successCallback);

Complete list of shortcut methods:

$http.get
$http.head
$http.post
$http.put
$http.delete
$http.jsonp
$http.patch
Community
  • 1
  • 1
michelem
  • 14,430
  • 5
  • 50
  • 66