如何在laravel 5中删除帖子资源? [英] How can I delete a post resource in laravel 5?
问题描述
Laravel 5版本
我正在使用新的laravel 5版本进行项目,由于某种原因我无法删除帖子,当我按Delete时,它会将我重定向到ID为/post/3的帖子显示页面,我得到了一张空白的白纸,当我返回索引视图时,我得到了所有帖子,并且该帖子尚未被删除.这是我下面的内容:
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:
发布迁移文件
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.');
}
}
}
据我所知,这还可以,但是我认为它的删除方法正在使这一切陷入困境.在我的路线文件中,我设置了路线资源,因此在php artisan
中显示路线,我可以看到销毁路线,如下所示:
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:
| | 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
带有删除按钮的表单
在这里,我有一个简单的按钮,它告诉表单从索引列表中删除资源并将其从表中获取.
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.
推荐答案
我不知道如何使用静态类 Form 来做到这一点,但几天前我仅使用HTML代码发现了此问题.
I don't know how to do it with the static class Form but I found this problem some days ago using only HTML code.
这不起作用:
<form action="{{ action('Controller@destroy') }}" method="DELETE">
...
</form>
这很好:
<form action="{{ action('Controller@destroy') }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
...
</form>
来源:
- http://laravel. io/forum/01-19-19-2015-删除路由-doesnt-工作页面未找到
- https://laracasts.com/discuss/channels/general-discussion/how-to-to-update-delete-using-forms-restful-controllers
- http://laravel.io/forum/01-19-2015-delete-route-doesnt-work-page-not-found
- https://laracasts.com/discuss/channels/general-discussion/how-to-updatedelete-using-forms-and-restful-controllers
这篇关于如何在laravel 5中删除帖子资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!