Home >Backend Development >PHP Tutorial >Laravel 5.1 表单删除操作

Laravel 5.1 表单删除操作

WBOY
WBOYOriginal
2016-06-23 13:25:41963browse

表单删除两种方法

1.Form表单操作

需要Laravel 安装laravelcollective

"require": {"php": ">=5.5.9","laravel/framework": "5.1.*","laravelcollective/html": "5.1.*"},

就可以使用{!! Form::open() !!}标签了
如下:

{!! Form::open(array('url' => route('admin.shop_category_tag.destroy',$shop_category_tag->id),'method'=>'delete')) !!}{!! Form::submit('删除',array('class' => 'btn btn-info btn-danger')) !!}{!! Form::close()!!}

如果没有安装

就使用HTML默认的表单提交删除,例如:

<form method="post" action="{{ route('admin.shop_category_tag.destroy',$shop_category_tag->id) }}" accept-charset="utf-8" id="hidden-delete-form">    <input name="_method" type="hidden" value="delete">    <input type="hidden" name="_token" value="{{ csrf_token() }}">    <button type="submit" class="btn btn-info btn-danger btn-sm iframe cboxElement" 删除</button></form>

控制器方法可以这么写:

public function destroy($id){$shop_category_tag = Shop_category_tag::find($id);    if($shop_category_tag->delete()){        return Redirect::back()->with('message', '删除成功!');    }else{        return Redirect::back()->withInput()->with('errors','删除失败!');    }}

路由:

Route::resource('shop_category_tag','ShopCategoryTagController');

2.控制器新建方法delete()

控制器内容和上面一致,不过需要注意的是路由要增加一条针对delete()方法的,这样会增加路由的复杂度,这里不推荐第二种方法。

原文地址:http://note.mango.im/article/8

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn