首页 > 解决方案 > laravel 删除表单不起作用

问题描述

@extends('layouts.app')
@section('content')
<div class="row">
    <div style="float:left;" class="col-md-8">
            <h3>Currency</h3>
    </div>
    <div style="float:right;" class="col-md-4">        
        <a href="/currency/create" class="btn btn-dark btn-sm ml-3" style="float: right"><i class="fa fa-edit" style="color:white;"></i>Add New</a>
        <input type="text" id="search" name="search" placeholder="search" class="form-control-sm col-md-8 ml-1" style="float:right;"><br><br>
    </div>   
</div>

    @if(count($currencies) > 0 )
    <table class="table table-striped table-sm" >
            <thead>
            <tr><th scope="col">ID</th><th scope="col">Currency</th><th scope="col">Country</th><th scope="col" colspan="2" style="text-align:right">Actions</th></tr>
            </thead>
        @foreach($currencies as $currency)        
            <tr><td scope="row">{{$currency->id}}</td>
                <td>{{$currency->title}}</td>
                <td>{{$currency->country}}</td>
                {!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
                {{Form::hidden('_method','DELETE')}}
                 @csrf
                    <td><button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>   
                    <a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a></td></tr>       
                {!!Form::close() !!}               
        @endforeach
    </table>
    {{$currencies->links()}}
    @else
        <p> No Data Available </p>    
    @endif

@endsection

大家好, 删除表单未在此代码中提交,我尝试了很多东西,但我无法弄清楚问题所在。请帮我解决一下这个。

标签: laravellaravelcollective

解决方案


在我看来,您的 html 是错误的,您打开、关闭表单并放置@csrftd标签之外,而提交按钮在其中

尝试将您的表单定义移动到td标签内,例如:

<td>
    {!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
        @csrf
        <button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>   
        <a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a>
    {!!Form::close() !!}    
</td>

推荐阅读