如何删除 Laravel 5 中的帖子资源?

2024-05-01

Laravel 5 版本

我正在开发一个新的 laravel 5 版本的项目,由于某种原因,我无法删除帖子,当我按删除时,它只会将我重定向到帖子显示页面,其 id 例如 /post/3 ,我得到一个空白的白色页面,当我返回索引视图时,我会看到所有帖子,并且该帖子尚未被删除。以下是我的内容:

发布迁移文件

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');
    }

}

帖子控制器

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要显示路线,我可以看到销毁路线,如下所示:

|        | 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

发布带有删除按钮的表单

在这里,我有一个简单的按钮,告诉表单从索引列表中删除资源并从表中获取它。

{!! 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() !!}

我得到的是什么,没有任何帖子被删除。任何答案或指出正确的地方都会很棒。


我不知道如何使用静态类来做到这一点Form但几天前我只使用 HTML 代码发现了这个问题。

这不起作用:

<form action="{{ action('Controller@destroy') }}" method="DELETE">
...
</form>

这工作正常:

<form action="{{ action('Controller@destroy') }}" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    ...
</form>

Sources:

  • http://laravel.io/forum/01-19-2015-delete-route-doesnt-work-page-not-found 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 https://laracasts.com/discuss/channels/general-discussion/how-to-updatedelete-using-forms-and-restful-controllers
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何删除 Laravel 5 中的帖子资源? 的相关文章

随机推荐