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

Laravel 5.1 表单删除操作

WBOY
Release: 2016-06-23 13:25:41
Original
952 people have browsed it

表单删除两种方法

1.Form表单操作

需要Laravel 安装laravelcollective

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

就可以使用{!! 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()!!}
Copy after login

如果没有安装

就使用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>
Copy after login

控制器方法可以这么写:

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','删除失败!');    }}
Copy after login

路由:

Route::resource('shop_category_tag','ShopCategoryTagController');
Copy after login

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template