如何删除 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 中的帖子资源? 的相关文章

  • “cross-env”不被识别为内部或外部命令,

    伙计们 你们能帮我解决这个问题吗 我在为我的 Laravel Mix 运行 npm run dev 时遇到问题 我点击了下面的链接 但仍然存在错误 我的操作系统有问题吗 我尝试删除节点模块 运行npm install global cros
  • PHP cURL 重定向到本地主机

    我正在尝试使用带有 cURL 的 php 脚本登录外部网页 我是 cURL 的新手 所以我觉得我错过了很多东西 我找到了几个例子并修改了它们以允许访问https页面 最终 我的目标是能够登录页面并在登录后通过指定的链接下载 csv 到目前为
  • 使用php在html页面中显示bbcode

    我已经有一个 bbcode 字符串 mybbcode b Hello word b 使用 php 我想在 html 页面中以 html 格式显示它 例如 div gt b hello word b div 基本上其他人已经对你说过了 但是如
  • 在 Drupal 中选择性地删除页面的样式表

    我正在尝试为首页制作不同的布局 在此过程中 我声明了名为 front page css 和 page front tpl php 的新样式表 我正在使用加载responsive sidebar css 的 Zen 子主题 我想删除 resp
  • 知道何时调用 persist

    我正在使用 Doctrine 2 作为我的 ORM 一切进展顺利 但我一直想知道EntityManager persist 方法 这 持久实体 https www doctrine project org projects doctrine
  • PHP 从日志事件中获取行号

    好的 我还有一个问题HERE https stackoverflow com questions 3213423 php how could i make this class better suggestions feedback wel
  • 为什么使用闭包进行赋值而不是直接将值赋给键?

    我当时正在看在 7 10 他添加了一个数据库依赖项并使用闭包来分配值 我的问题是为什么不直接使用直接赋值 我的意思是不这样做 container db capsule 相当于这样做 container db function contain
  • 用PHP动态生成二维码[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在尝试在我的网站上生成二维码 他们所要做的就是在其中包含一个 URL 我网站上的变量将提供该 URL
  • filter_input() 何时删除 POST 变量的斜杠?

    我创建了一个小型 PHP 脚本 它在 PHP 5 2 17 的服务器上运行magic quotes gpc指令已启用 我没有对 php ini 文件的写访问权限 并且我想从用户输入中删除所有斜杠 即使magic quotes gpc指令被关
  • Laravel 白名单域认证

    我正在寻找只允许某些域访问我的 laravel 应用程序的最佳方法 我目前正在使用 Laravel 5 1 并且如果引用域不在白名单域中 则使用中间件进行重定向 class Whitelist Handle an incoming requ
  • 根据类别 woocommerce 更改同一产品的默认变体值

    我正在研究一种根据其所属类别显示同一产品的默认变体值的方法 例如 我出售一张带有蓝色和红色选项的卡 当用户进入 一 类别时 我希望默认值为蓝色 如果他属于第二类 则该值将为红色 我发现了一个钩子woocommerce product def
  • Laravel 类邮件程序不存在

    我将应用程序从 5 更新到 5 2 现在 当我调用 Mail send 时 它会返回一个异常 Class mailer 不存在 Mail send emails mail data gt content function m use to
  • 如何缓存 twitter api 结果?

    我想缓存 twitter api 结果的结果并将其显示给用户 缓存结果的最佳方法是什么 我正在考虑根据时间限制将结果写入文件 可以吗 还是应该使用任何其他方法 最重要的是 理想的缓存时间是多少 我想显示来自 twitter 的最新内容 但
  • 扫描 PHP 上传的病毒

    我目前正在使用以下代码来扫描作为申请表的一部分上传的文件 safe path escapeshellarg dir file command usr bin clamscan stdout safe path out int 1 exec
  • PHP:展平数组-最快的方法? [复制]

    这个问题在这里已经有答案了 是否有任何快速方法可以在不运行 foreach 循环的情况下展平数组并选择子键 在本例中为 键 和 值 或者 foreach 始终是最快的方法 Array 0 gt Array key gt string val
  • 我应该使用排队系统来处理付款吗?

    我在用着Slim https www slimframework com和这个结合Stripe 的 PHP 库 https stripe com docs api php在我的应用程序中处理付款 一切都很好 但是直到最近 我在我的系统中发现
  • 连接 3 三张表

    我有这个图表应该可以解释我的情况 我需要一些关于连接 3 个表的帮助 我不知道如何做这种事情 因此 我可以通过执行以下操作来经历一段检索记录的 while 循环 img src alt Album AlbumID 使用内部联接 http w
  • PHP 中的简单 JSON 请求

    我有以下 json country code latitude 45 9390 longitude 24 9811 zoom 6 address city country Romania country code RO region 我只想
  • 使用“AND”表达式构建动态 SQL,而不混淆嵌套条件?

    总的来说 我对 php 和编码相当陌生 我有一系列条件需要测试它们是否已设置 它们是 option1 option2 option3 if isset option1 if isset option2 if isset option3 qu
  • 在 PHP 命令行上显示完整的堆栈跟踪

    Problem 我的 PHP 堆栈跟踪缩写为 Stack trace 0 www html table app create php 128 SoapClient gt call call Array 1 www html table ap

随机推荐