gpt4 book ai didi

php - 如何删除 laravel 5 中的帖子资源?

转载 作者:可可西里 更新时间:2023-10-31 22:53:56 26 4
gpt4 key购买 nike

Laravel 5 版本

我正在使用新的 laravel 5 版本开发一个项目,由于某种原因我无法删除帖子,当我按下删除键时,它只是将我重定向到 ID 为/post/3 的帖子展示页面,我得到一个空白的白页,当我返回索引 View 时,我得到了所有的帖子,而且那个帖子还没有被删除。这是我下面的内容:

帖子迁移文件

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

}

帖子 Controller

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

}

据我所知,这没问题,但我认为是 delete 方法搞砸了这一切。在我的路由文件中,我设置了路由资源,因此在 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>

来源:

关于php - 如何删除 laravel 5 中的帖子资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com