Laravel链接破坏未定义的路线

时间:2017-09-08 13:11:53

标签: php laravel routes

enter image description here Heloo伙计们我想在laravel中销毁一个模型。所有它没关系,但是给了我“路线[finance / loans.destroy,$ loan-> id]未定义。” 你能帮助我吗? 这是删除元素的按钮:

<td><button href="{{route('finance/loans.destroy,$loan->id') }}" id="btnExc" class="btn btn-sm btn-danger glyphicon glyphicon-trash" accesskey="x" hidden><br>Delete</button></td>

这些是路线:

|        | POST      | finance/loans             | loans.store      | App\Http\Controllers\loanController@store                              | web,auth     |
|        | GET|HEAD  | finance/loans             | loans.index      | App\Http\Controllers\loanController@index                              | web,auth     |
|        | GET|HEAD  | finance/loans/create      | loans.create     | App\Http\Controllers\loanController@create                             | web,auth     |
|        | PUT|PATCH | finance/loans/{loan}      | loans.update     | App\Http\Controllers\loanController@update                             | web,auth     |
|        | GET|HEAD  | finance/loans/{loan}      | loans.show       | App\Http\Controllers\loanController@show                               | web,auth     |
|        | DELETE    | finance/loans/{loan}      | loans.destroy    | App\Http\Controllers\loanController@destroy                            | web,auth     |
|        | GET|HEAD  | finance/loans/{loan}/edit | loans.edit       | App\Http\Controllers\loanController@edit 

路线声明:

Route::resource('/finance/loans','loanController'); 

2 个答案:

答案 0 :(得分:0)

您正在调用路线错误,并且缺少引号:

<td>
<button href="{{ route('loans.destroy', $loan->id) }}" id="btnExc" 
        class="btn btn-sm btn-danger glyphicon glyphicon-trash" accesskey="x" hidden>
    <br>Delete</button>
</td>

修改

如果您不通过表单发布,则无法访问DELETE http请求。如果您想使用按钮直接删除模型,您必须制作自己的方法和路线:

Route:get('finance/loans/destroyMe/{id}', 'loanController@destroyMe');

然后在控制器中:

public function destroyMe($id)
{
    SomeModel::destroy($id);
}

这只是抄写代码,没有测试出来,但这是基本原则

答案 1 :(得分:0)

你应该以laravel方式(_method='DELETE')发送带有删除方法的表格(js与否)。 检查一下

<td>
<form action="{{ route('loans.destroy', $loan->id) }}" method="post">
 <input type="hidden" name="_method" value="DELETE" />
<input type="submit"  value="Delete" name="Delete" id="btnExc" 
        class="btn btn-sm btn-danger glyphicon glyphicon-trash" accesskey="x"/>

</form>
</td>