-1

I am currently using laravel 5.3 with angular. I have few function such as add / edit / delete posts now I decided to have another function which deletes all posts with just a click.

Somehow I checked that everything works up to my service part but after that it doesn't seem to be passing to my controller in laravel.

Can someone let me know if I am using the wrong syntax for angular or laravel?

I have this in web.php

Route::resource('posts', 'PostController');
Route::delete('posts/destroyall', array('as' => 'posts.destroyall', 'uses' => 'PostController@destroyall'));

this in my PostController.php

public function destroyall() {
    Post::truncate();
    return response()->json(Post::get());
}

this for my controller in angular

    $scope.destroyAll = function () {
        Post.destroyAll()
            .success(function () {
                // Use the get() created in Post service
                // Gets all the posts
                Post.get()
                    .success(function(getData) {
                        $scope.posts = (getData);
                    });
            })
    }

This in my service

        destroyAll : function () {
            return $http.delete('/posts/destroyall/');
        }

can someone give me an advice please?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
WXR
  • 481
  • 1
  • 7
  • 18
  • Are You Storing It In Database(Table Data)? – Shankar Shastri Oct 26 '16 at 02:58
  • @ShankarShastri yes, stored in DB so wanted to clear the whole table which is the easiest way to delete all – WXR Oct 26 '16 at 03:01
  • 1
    Then Just Have A Single Route Request To Your PHP And Then Invoke Truncate SQL Statement And Truncate The Data. – Shankar Shastri Oct 26 '16 at 03:03
  • @ShankarShastri so that's like skip angular and just do it directly from front to back? But wouldn't that make the page refresh though? – WXR Oct 26 '16 at 03:07
  • 1
    $scope.posts is where you are displaying your posts right?If Yes Then Just Update The $scope.posts to null; – Shankar Shastri Oct 26 '16 at 03:12
  • @ShankarShastri oh that yes, I can get that part but just somehow in DB it wouldn't update. that's why I mentioned up to the point before `return $http.delete('/posts/destroyall/');` I still can try `console.log` but when I try to do anything from the backend nothing happens – WXR Oct 26 '16 at 03:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126683/discussion-between-shankar-shastri-and-wxr). – Shankar Shastri Oct 26 '16 at 03:20
  • Check this post http://stackoverflow.com/questions/33054706/truncate-a-table-in-laravel-5 Post::::query()->truncate(); – Bas Oct 26 '16 at 03:30
  • @Bas I read that page already and no help but I figured what's wrong now – WXR Oct 26 '16 at 03:41

1 Answers1

1

it was a stupid mistake but I think this might happen often.

This is because of laravel's routing positions.

I need my routing to be

Route::delete('posts/destroyall', 'PostController@destroyall');
Route::resource('posts', 'PostController');

I had resource then delete that's why it didn't work. Switch it around then it works fine now.

WXR
  • 481
  • 1
  • 7
  • 18
  • That's probably because in the resource you also can call the delete :) So you can post it to the controller and make use of that method – Bas Oct 26 '16 at 10:48