DELETE request in laravel










2















I have a categories page and create a remove button which to remove it the categories, the image as below:
enter image description here



So the problem is show me 'MethodNotAllowedHttpException'



Alright. Here is Route file



Route::delete('removeCategory/id','AdminController@removeCategory');


Controller file



 public function removeCategory(Request $id)
$cats = cats::find($id);
$cats->delete();




and View file



@foreach($data as $product)
<tr style="height:50px">
<td style="padding:10px">$product->cat_name</td>
<td><a class="btn btn-sm btn-fill btn-primary"
href="url('/admin/editCategory')/$product->id">Edit</a></td>
<td><a href="url('admin/removeCategory')/$product->id" onclick="return confirm('Are you sure?')"
class="btn btn-sm btn-fill btn-primary">Remove</a></td>
</tr>
@endforeach


Thanks for anyone share the info to me, I have tried it but showing me this error message.
enter image description here










share|improve this question
























  • Don't forget to use !! method_field('delete') !!

    – Peon
    Nov 15 '18 at 14:44












  • Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

    – Peon
    Nov 15 '18 at 14:47















2















I have a categories page and create a remove button which to remove it the categories, the image as below:
enter image description here



So the problem is show me 'MethodNotAllowedHttpException'



Alright. Here is Route file



Route::delete('removeCategory/id','AdminController@removeCategory');


Controller file



 public function removeCategory(Request $id)
$cats = cats::find($id);
$cats->delete();




and View file



@foreach($data as $product)
<tr style="height:50px">
<td style="padding:10px">$product->cat_name</td>
<td><a class="btn btn-sm btn-fill btn-primary"
href="url('/admin/editCategory')/$product->id">Edit</a></td>
<td><a href="url('admin/removeCategory')/$product->id" onclick="return confirm('Are you sure?')"
class="btn btn-sm btn-fill btn-primary">Remove</a></td>
</tr>
@endforeach


Thanks for anyone share the info to me, I have tried it but showing me this error message.
enter image description here










share|improve this question
























  • Don't forget to use !! method_field('delete') !!

    – Peon
    Nov 15 '18 at 14:44












  • Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

    – Peon
    Nov 15 '18 at 14:47













2












2








2


1






I have a categories page and create a remove button which to remove it the categories, the image as below:
enter image description here



So the problem is show me 'MethodNotAllowedHttpException'



Alright. Here is Route file



Route::delete('removeCategory/id','AdminController@removeCategory');


Controller file



 public function removeCategory(Request $id)
$cats = cats::find($id);
$cats->delete();




and View file



@foreach($data as $product)
<tr style="height:50px">
<td style="padding:10px">$product->cat_name</td>
<td><a class="btn btn-sm btn-fill btn-primary"
href="url('/admin/editCategory')/$product->id">Edit</a></td>
<td><a href="url('admin/removeCategory')/$product->id" onclick="return confirm('Are you sure?')"
class="btn btn-sm btn-fill btn-primary">Remove</a></td>
</tr>
@endforeach


Thanks for anyone share the info to me, I have tried it but showing me this error message.
enter image description here










share|improve this question
















I have a categories page and create a remove button which to remove it the categories, the image as below:
enter image description here



So the problem is show me 'MethodNotAllowedHttpException'



Alright. Here is Route file



Route::delete('removeCategory/id','AdminController@removeCategory');


Controller file



 public function removeCategory(Request $id)
$cats = cats::find($id);
$cats->delete();




and View file



@foreach($data as $product)
<tr style="height:50px">
<td style="padding:10px">$product->cat_name</td>
<td><a class="btn btn-sm btn-fill btn-primary"
href="url('/admin/editCategory')/$product->id">Edit</a></td>
<td><a href="url('admin/removeCategory')/$product->id" onclick="return confirm('Are you sure?')"
class="btn btn-sm btn-fill btn-primary">Remove</a></td>
</tr>
@endforeach


Thanks for anyone share the info to me, I have tried it but showing me this error message.
enter image description here







javascript php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:17







lun7code

















asked Nov 15 '18 at 14:37









lun7codelun7code

548




548












  • Don't forget to use !! method_field('delete') !!

    – Peon
    Nov 15 '18 at 14:44












  • Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

    – Peon
    Nov 15 '18 at 14:47

















  • Don't forget to use !! method_field('delete') !!

    – Peon
    Nov 15 '18 at 14:44












  • Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

    – Peon
    Nov 15 '18 at 14:47
















Don't forget to use !! method_field('delete') !!

– Peon
Nov 15 '18 at 14:44






Don't forget to use !! method_field('delete') !!

– Peon
Nov 15 '18 at 14:44














Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

– Peon
Nov 15 '18 at 14:47





Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request.

– Peon
Nov 15 '18 at 14:47












4 Answers
4






active

oldest

votes


















7














Since You want to do it without complicating the code with ajax,



solution is simply sending POST request and defining DELETE method as hidden field.



For simplicity You can add that field using method_field helper:



@foreach($data as $product)
<tr style="height:50px">
<td style="padding:10px">$product->cat_name</td>
<td><a class="btn btn-sm btn-fill btn-primary"
href="url('/admin/editCategory')/$product->id">Edit</a></td>

<td>
<form
method="post"
action="url(''admin/removeCategory')/$product->id">

!! Form::token() !!
method_field('DELETE')

<button
type="submit"
onclick="return confirm('Are you sure?')"
class="btn btn-sm btn-fill btn-primary">Remove</button>
</form>
</td>

</tr>
@endforeach


and make sure after deleting object it's returning back to listing:



public function removeCategory($id) 
$Cat = cats::find($id);
if ($Cat)
$Cat->delete();

return redirect()->back();






share|improve this answer

























  • Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

    – lun7code
    Nov 15 '18 at 15:19






  • 1





    thanks for everyone especially answer from num8er and Devon! :)

    – lun7code
    Nov 15 '18 at 15:48











  • @lun7code it worked?

    – num8er
    Nov 15 '18 at 15:57






  • 1





    yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

    – lun7code
    Nov 16 '18 at 2:03


















7














MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.



https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405






share|improve this answer






























    1














    Try editing your route to this:
    Route::get('removeCategory/id','AdminController@removeCategory');



    Then edit your controller to this:



    public function removeCategory($id)
    $cats = cats::find($id);
    $cats->delete();
    return response(['Message' => 'This request has been deleted'], 200);



    This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.






    share|improve this answer






























      1














      1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.


      2. You are also type hinting $id as a Request object in the controller method.
        Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:



       public function removeCategory($id) 







      share|improve this answer























      • lol.....remove the request is works! thanks!

        – lun7code
        Nov 15 '18 at 15:47










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321805%2fdelete-request-in-laravel%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      Since You want to do it without complicating the code with ajax,



      solution is simply sending POST request and defining DELETE method as hidden field.



      For simplicity You can add that field using method_field helper:



      @foreach($data as $product)
      <tr style="height:50px">
      <td style="padding:10px">$product->cat_name</td>
      <td><a class="btn btn-sm btn-fill btn-primary"
      href="url('/admin/editCategory')/$product->id">Edit</a></td>

      <td>
      <form
      method="post"
      action="url(''admin/removeCategory')/$product->id">

      !! Form::token() !!
      method_field('DELETE')

      <button
      type="submit"
      onclick="return confirm('Are you sure?')"
      class="btn btn-sm btn-fill btn-primary">Remove</button>
      </form>
      </td>

      </tr>
      @endforeach


      and make sure after deleting object it's returning back to listing:



      public function removeCategory($id) 
      $Cat = cats::find($id);
      if ($Cat)
      $Cat->delete();

      return redirect()->back();






      share|improve this answer

























      • Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

        – lun7code
        Nov 15 '18 at 15:19






      • 1





        thanks for everyone especially answer from num8er and Devon! :)

        – lun7code
        Nov 15 '18 at 15:48











      • @lun7code it worked?

        – num8er
        Nov 15 '18 at 15:57






      • 1





        yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

        – lun7code
        Nov 16 '18 at 2:03















      7














      Since You want to do it without complicating the code with ajax,



      solution is simply sending POST request and defining DELETE method as hidden field.



      For simplicity You can add that field using method_field helper:



      @foreach($data as $product)
      <tr style="height:50px">
      <td style="padding:10px">$product->cat_name</td>
      <td><a class="btn btn-sm btn-fill btn-primary"
      href="url('/admin/editCategory')/$product->id">Edit</a></td>

      <td>
      <form
      method="post"
      action="url(''admin/removeCategory')/$product->id">

      !! Form::token() !!
      method_field('DELETE')

      <button
      type="submit"
      onclick="return confirm('Are you sure?')"
      class="btn btn-sm btn-fill btn-primary">Remove</button>
      </form>
      </td>

      </tr>
      @endforeach


      and make sure after deleting object it's returning back to listing:



      public function removeCategory($id) 
      $Cat = cats::find($id);
      if ($Cat)
      $Cat->delete();

      return redirect()->back();






      share|improve this answer

























      • Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

        – lun7code
        Nov 15 '18 at 15:19






      • 1





        thanks for everyone especially answer from num8er and Devon! :)

        – lun7code
        Nov 15 '18 at 15:48











      • @lun7code it worked?

        – num8er
        Nov 15 '18 at 15:57






      • 1





        yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

        – lun7code
        Nov 16 '18 at 2:03













      7












      7








      7







      Since You want to do it without complicating the code with ajax,



      solution is simply sending POST request and defining DELETE method as hidden field.



      For simplicity You can add that field using method_field helper:



      @foreach($data as $product)
      <tr style="height:50px">
      <td style="padding:10px">$product->cat_name</td>
      <td><a class="btn btn-sm btn-fill btn-primary"
      href="url('/admin/editCategory')/$product->id">Edit</a></td>

      <td>
      <form
      method="post"
      action="url(''admin/removeCategory')/$product->id">

      !! Form::token() !!
      method_field('DELETE')

      <button
      type="submit"
      onclick="return confirm('Are you sure?')"
      class="btn btn-sm btn-fill btn-primary">Remove</button>
      </form>
      </td>

      </tr>
      @endforeach


      and make sure after deleting object it's returning back to listing:



      public function removeCategory($id) 
      $Cat = cats::find($id);
      if ($Cat)
      $Cat->delete();

      return redirect()->back();






      share|improve this answer















      Since You want to do it without complicating the code with ajax,



      solution is simply sending POST request and defining DELETE method as hidden field.



      For simplicity You can add that field using method_field helper:



      @foreach($data as $product)
      <tr style="height:50px">
      <td style="padding:10px">$product->cat_name</td>
      <td><a class="btn btn-sm btn-fill btn-primary"
      href="url('/admin/editCategory')/$product->id">Edit</a></td>

      <td>
      <form
      method="post"
      action="url(''admin/removeCategory')/$product->id">

      !! Form::token() !!
      method_field('DELETE')

      <button
      type="submit"
      onclick="return confirm('Are you sure?')"
      class="btn btn-sm btn-fill btn-primary">Remove</button>
      </form>
      </td>

      </tr>
      @endforeach


      and make sure after deleting object it's returning back to listing:



      public function removeCategory($id) 
      $Cat = cats::find($id);
      if ($Cat)
      $Cat->delete();

      return redirect()->back();







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 17:51

























      answered Nov 15 '18 at 14:40









      num8ernum8er

      11.9k22240




      11.9k22240












      • Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

        – lun7code
        Nov 15 '18 at 15:19






      • 1





        thanks for everyone especially answer from num8er and Devon! :)

        – lun7code
        Nov 15 '18 at 15:48











      • @lun7code it worked?

        – num8er
        Nov 15 '18 at 15:57






      • 1





        yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

        – lun7code
        Nov 16 '18 at 2:03

















      • Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

        – lun7code
        Nov 15 '18 at 15:19






      • 1





        thanks for everyone especially answer from num8er and Devon! :)

        – lun7code
        Nov 15 '18 at 15:48











      • @lun7code it worked?

        – num8er
        Nov 15 '18 at 15:57






      • 1





        yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

        – lun7code
        Nov 16 '18 at 2:03
















      Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

      – lun7code
      Nov 15 '18 at 15:19





      Yup. I have tried the method you sharing to do it, but it still showing MethodNotAllowedHttpException error even I do it through a form.

      – lun7code
      Nov 15 '18 at 15:19




      1




      1





      thanks for everyone especially answer from num8er and Devon! :)

      – lun7code
      Nov 15 '18 at 15:48





      thanks for everyone especially answer from num8er and Devon! :)

      – lun7code
      Nov 15 '18 at 15:48













      @lun7code it worked?

      – num8er
      Nov 15 '18 at 15:57





      @lun7code it worked?

      – num8er
      Nov 15 '18 at 15:57




      1




      1





      yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

      – lun7code
      Nov 16 '18 at 2:03





      yes. Indeed. Is a correct answer and I vote for the acceptance answer so anyone those have same issue can be use as reference.

      – lun7code
      Nov 16 '18 at 2:03













      7














      MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.



      https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405






      share|improve this answer



























        7














        MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.



        https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405






        share|improve this answer

























          7












          7








          7







          MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.



          https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405






          share|improve this answer













          MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.



          https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 14:39









          messerbillmesserbill

          3,1031225




          3,1031225





















              1














              Try editing your route to this:
              Route::get('removeCategory/id','AdminController@removeCategory');



              Then edit your controller to this:



              public function removeCategory($id)
              $cats = cats::find($id);
              $cats->delete();
              return response(['Message' => 'This request has been deleted'], 200);



              This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.






              share|improve this answer



























                1














                Try editing your route to this:
                Route::get('removeCategory/id','AdminController@removeCategory');



                Then edit your controller to this:



                public function removeCategory($id)
                $cats = cats::find($id);
                $cats->delete();
                return response(['Message' => 'This request has been deleted'], 200);



                This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.






                share|improve this answer

























                  1












                  1








                  1







                  Try editing your route to this:
                  Route::get('removeCategory/id','AdminController@removeCategory');



                  Then edit your controller to this:



                  public function removeCategory($id)
                  $cats = cats::find($id);
                  $cats->delete();
                  return response(['Message' => 'This request has been deleted'], 200);



                  This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.






                  share|improve this answer













                  Try editing your route to this:
                  Route::get('removeCategory/id','AdminController@removeCategory');



                  Then edit your controller to this:



                  public function removeCategory($id)
                  $cats = cats::find($id);
                  $cats->delete();
                  return response(['Message' => 'This request has been deleted'], 200);



                  This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 14:49









                  Solomon AntoineSolomon Antoine

                  172110




                  172110





















                      1














                      1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.


                      2. You are also type hinting $id as a Request object in the controller method.
                        Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:



                       public function removeCategory($id) 







                      share|improve this answer























                      • lol.....remove the request is works! thanks!

                        – lun7code
                        Nov 15 '18 at 15:47















                      1














                      1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.


                      2. You are also type hinting $id as a Request object in the controller method.
                        Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:



                       public function removeCategory($id) 







                      share|improve this answer























                      • lol.....remove the request is works! thanks!

                        – lun7code
                        Nov 15 '18 at 15:47













                      1












                      1








                      1







                      1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.


                      2. You are also type hinting $id as a Request object in the controller method.
                        Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:



                       public function removeCategory($id) 







                      share|improve this answer













                      1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.


                      2. You are also type hinting $id as a Request object in the controller method.
                        Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:



                       public function removeCategory($id) 








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 14:51









                      DevonDevon

                      23.5k42747




                      23.5k42747












                      • lol.....remove the request is works! thanks!

                        – lun7code
                        Nov 15 '18 at 15:47

















                      • lol.....remove the request is works! thanks!

                        – lun7code
                        Nov 15 '18 at 15:47
















                      lol.....remove the request is works! thanks!

                      – lun7code
                      Nov 15 '18 at 15:47





                      lol.....remove the request is works! thanks!

                      – lun7code
                      Nov 15 '18 at 15:47

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321805%2fdelete-request-in-laravel%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands