Manas
Manas

Reputation: 3330

Delete action is not working in laravel?

I have a form which has the delete button in it.

 <form method="post" id="delform" action="{{ url('/templates/delete',[$template->template_id]) }}">
    {{ method_field('DELETE') }}
  {{ csrf_field() }}

 <input type="submit" class="btn btn-danger" id="del" value="delete>

  </form>

then in my routes web.php:

Route::delete('/templates/delete/{id}','TemplateController@delete');

when i click on delete i get this error:

enter image description here

what am i doing wong please help? thanks in advance

I have added my composer.json here:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "maatwebsite/excel": "~2.1.0"        "rmccue/requests": ">=1.0"    },
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.7"
},
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},
"scripts": {
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
},
"config": {
    "preferred-install": "dist",
    "sort-packages": true
}
}

here is the console logs:

enter image description here

Upvotes: 1

Views: 730

Answers (1)

Thiago Fran&#231;a
Thiago Fran&#231;a

Reputation: 1847

You forgot double-quotes to close value from input submit

<input type="submit" ... value="delete">

and a comma in composer json after package:

"maatwebsite/excel": "~2.1.0",

Finally HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method.

<input type="hidden" name="_method" value="DELETE">

You may use the method_field helper to generate the _method input:

{{ method_field('DELETE') }}

Laravel routes documentation

Look at rest conventions

Upvotes: 1

Related Questions