laravel 如何删除laravel 5中的帖子资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28634798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete a post resource in laravel 5?
提问by M dunbavan
Laravel 5 version
Laravel 5 版本
I am working on a project with the new laravel 5 release and for some reason i cannot delete a post, when I press delete it just redirects me to the post show page with the id such as /post/3 and I get a blank white page, when I go back to index view I get all the posts and that one has not been deleted. Here is what I have below:
我正在使用新的 laravel 5 版本开发一个项目,由于某种原因我无法删除帖子,当我按下删除它只会将我重定向到带有诸如 /post/3 之类的 ID 的帖子显示页面,我得到一个空白的白色页面,当我回到索引视图时,我得到了所有的帖子,而且那个帖子还没有被删除。这是我在下面的内容:
Posts migration file
帖子迁移文件
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function(Blueprint $table)
{
$table->increments('id');
$table->string('title')->default('');
$table->string('slug')->default('');
$table->text('body');
$table->integer('author_id');
$table->string('author');
$table->string('featured_image');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}
}
PostsController
帖子控制器
use Input;
use Redirect;
use Storage;
use SirTrevorJs;
use STConverter;
use Validator;
use Image;
use Boroughcc\Post;
use Boroughcc\Http\Requests;
use Boroughcc\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostsController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Post $post)
{
//
$this->middleware('auth');
$input = array_except(Input::all(), '_method');
$post->update($input);
return Redirect::route('posts.show', $post->slug)->with('message', 'Post updated.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy(Post $post)
{
//
//$post = Post::findOrFail($id);
$post->delete();
if($post->delete()) {
return Redirect::route('posts.index')->with('message', 'Post deleted.');
}
}
}
As far as i am aware, this is okay but I think its the delete method that is screwing with it all. In my routes file I have the route resources set up so in php artisan
to display routes I can see the destroy route like so:
据我所知,这没关系,但我认为它的删除方法正在搞砸。在我的路线文件中,我设置了路线资源php artisan
以显示路线,我可以像这样看到销毁路线:
| | GET|HEAD | posts | posts.index | Boroughcc\Http\Controllers\PostsController@index | |
| | GET|HEAD | posts/create | posts.create | Boroughcc\Http\Controllers\PostsController@create | |
| | POST | posts | posts.store | Boroughcc\Http\Controllers\PostsController@store | |
| | GET|HEAD | posts/{posts} | posts.show | Boroughcc\Http\Controllers\PostsController@show | |
| | GET|HEAD | posts/{posts}/edit | posts.edit | Boroughcc\Http\Controllers\PostsController@edit | |
| | PUT | posts/{posts} | posts.update | Boroughcc\Http\Controllers\PostsController@update | |
| | PATCH | posts/{posts} | | Boroughcc\Http\Controllers\PostsController@update | |
| | DELETE | posts/{posts} | posts.destroy | Boroughcc\Http\Controllers\PostsController@destroy
Post form with delete button
发布带有删除按钮的表单
In here I have got a simple button that tells the form to delete the resource from the index list and take it from the table.
在这里,我有一个简单的按钮,它告诉表单从索引列表中删除资源并将其从表中取出。
{!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('posts.destroy', $post->id))) !!}
<li>
<img src="{!! $post->featured_image !!}" alt="" />
<a href="{{ route('posts.show', $post->id) }}">{{ $post->title }}</a>
</li>
(
{!! link_to_route('posts.edit', 'Edit', array($post->slug), array('class' => 'btn btn-info')) !!},
{!! Form::submit('Delete', array('class' => 'btn btn-danger')) !!}
)
{!! Form::close() !!}
What I am getting is nothing, no post gets deleted at all. Any answers or points to the right place would be awesome.
我得到的是什么,根本没有帖子被删除。任何答案或指向正确位置的点都会很棒。
回答by Samuele Colombo
I don't know how to do it with the static class Formbut I found this problem some days ago using only HTML code.
我不知道如何使用静态类Form但几天前我只使用 HTML 代码发现了这个问题。
This doesn't work:
这不起作用:
<form action="{{ action('Controller@destroy') }}" method="DELETE">
...
</form>
This works fine:
这工作正常:
<form action="{{ action('Controller@destroy') }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
...
</form>
Sources:
资料来源:
回答by Marcin Nabia?ek
You want to use Route model bindinghere.
您想在此处使用Route 模型绑定。
You can go to the boot
method in your app/Providers/RouteServiceProvider.php
file and add in int:
您可以转到文件中的boot
方法app/Providers/RouteServiceProvider.php
并添加int:
$router->bind('posts','Boroughcc\Post');
to tell laravel the posts
parameter (it's visible in route:list
you provided) should be bound to the
Post` model (Laravel by default will use id)
告诉 Laravelposts
参数(它在route:list
你提供的) should be bound to the
Post` 模型中可见(Laravel 默认使用 id)
or you can do the same in app/Http/routes.php
using:
或者您可以app/Http/routes.php
使用以下方法执行相同操作:
Router::bind('posts','Boroughcc\Post');
回答by mdamia
to delete a model, do this
要删除模型,请执行以下操作
$model -> find($id);
$model -> delete();
or simply
或者干脆
$model -> destroy($id);
to delete multiple rows
删除多行
$model -> destroy ([1,2,3]);
回答by Raul Fernandez Marques
how to send form by post applied to users:
如何通过应用于用户的邮件发送表单:
View:
看法:
<form class="" action="{{ url('/home/destroy') }}" method="post">
<li><button type="submit"><i class="fa fa-btn fa-sign-out"></i>Destroy user</button></li>
<input type="hidden" name="id" value="{{Auth::user()->id}}">
{{ method_field('DELETE') }}
{!! csrf_field() !!}
</form>
Route:
路线:
Route::group(['middleware' => 'web'], function () {
Route::delete('/home/destroy',[
'as'=> 'home.destroy',
'uses'=> 'homeController@destroy',
]);
});
Controller:
控制器:
use app\User;
use Illuminate\Support\Facades\Auth;
protected function destroy(Request $request)
{
$id= $request->id;
if (Auth::user()->id == $id){
$user= User::find($id);
$user-> delete();
Auth::logout();
return redirect()->route('welcome');
}else{
return redirect()->route('home');
}
}
if() is only to prevent a user deletes another and Auth::logout() is to clear user data in the session if you do not work with users will not need them, you must use == not === to compare $id and Auth::user()->id, $id works as string, and Auth::user()->id works as number.
if() 只是为了防止一个用户删除另一个用户而 Auth::logout() 是清除会话中的用户数据如果你不使用用户将不需要他们,你必须使用 == 不是 === 来比较 $ id 和 Auth::user()->id,$id 作为字符串工作,而 Auth::user()->id 作为数字工作。
This way is equivalent to using get with this code:
这种方式等效于使用带有以下代码的 get :
View:
看法:
<li><a href="{{ url('/home/delete', Auth::user()->id) }}"><i class="fa fa-btn fa-sign-out"></i>Delete user</a></li>
Route:
路线:
Route::group(['middleware' => 'web'], function () {
Route::get('/home/delete/{id}', 'homeController@delete');
});
Controller:
控制器:
protected function delete($id)
{
if (Auth::user()->id == $id){
$user= User::find($id);
$user-> delete();
Auth::logout();
return redirect()->route('welcome');
}else{
return redirect()->route('home');
}
}