M d*_*van 4 php laravel laravel-routing laravel-5
Laravel 5版
我正在开发一个带有新laravel 5版本的项目,由于某些原因我无法删除帖子,当我按下删除它只是将我重定向到带有id/post/3等id的帖子显示页面,我得到一个空白的白色页面,当我回到索引视图时,我收到了所有帖子,而且还没有删除.以下是我的内容:
发布迁移文件
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');
}
}
Run Code Online (Sandbox Code Playgroud)
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.');
}
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这是可以的,但我认为它的删除方法正在搞砸所有.在我的路由文件中,我设置了路由资源,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
Run Code Online (Sandbox Code Playgroud)
使用删除按钮发布表单
在这里,我有一个简单的按钮,告诉表单从索引列表中删除资源并从表中获取它.
{!! 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() !!}
Run Code Online (Sandbox Code Playgroud)
我得到的是什么,没有任何帖子被删除.任何答案或指向正确的地方都会很棒.
我不知道如何使用静态类Form,但我几天前只使用HTML代码发现了这个问题.
这不起作用:
<form action="{{ action('Controller@destroy') }}" method="DELETE">
...
</form>
Run Code Online (Sandbox Code Playgroud)
这很好用:
<form action="{{ action('Controller@destroy') }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
...
</form>
Run Code Online (Sandbox Code Playgroud)
资料来源:
您想在此处使用路由模型绑定。
您可以转到文件boot中的方法app/Providers/RouteServiceProvider.php并添加 int:
$router->bind('posts','Boroughcc\Post');
Run Code Online (Sandbox Code Playgroud)
告诉 Laravelposts参数(它在route:list您提供的) should be bound to thePost` 模型中可见(Laravel 默认情况下将使用 id)
或者您可以app/Http/routes.php使用以下方法执行相同操作:
Router::bind('posts','Boroughcc\Post');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15543 次 |
| 最近记录: |