Php 为什么laravel删除和显示方法在laravel 5中不起作用?
我有足够的restful资源以及它们是如何工作的,并且已经用restful构建了rails应用程序。我完全理解这一切的前提以及它们是如何工作的,但是不管我做什么,laravel都不工作 我为每种方法提供了一组资源,例如:Php 为什么laravel删除和显示方法在laravel 5中不起作用?,php,post,laravel,laravel-routing,laravel-5,Php,Post,Laravel,Laravel Routing,Laravel 5,我有足够的restful资源以及它们是如何工作的,并且已经用restful构建了rails应用程序。我完全理解这一切的前提以及它们是如何工作的,但是不管我做什么,laravel都不工作 我为每种方法提供了一组资源,例如: GET|HEAD | posts | posts.index | Boroughcc\Http\Controllers\PostsController@index GET|HEAD | posts/create | posts.create
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
正如你所看到的,这很简单,对吗?我基本上无法在post/url标题页上显示任何数据,也无法在/posts索引页上删除帖子。当我试图删除它时,它会在消息中显示“已删除帖子”,但它仍然存在;当我查看帖子显示url时,它会显示空白页面,数据应该在哪里,但没有显示;当我访问该帖子的url/编辑时,它在字段中不会显示任何内容。基本上都是空白的
在index posts页面上,我可以看到以下内容,这是正确的:
在单视图上,我得到以下结果:
应该有一个标题在灰色栏和一个上面的图像与正文文字下面的所有,但我看不到任何东西
我正在使用eloquent sluggable包创建slug,并如上所述访问它们
这是我的帖子管理员:
<?php namespace Boroughcc\Http\Controllers;
use Input;
use Redirect;
use Storage;
use SirTrevorJs;
use STConverter;
use Validator;
use Image;
use Boroughcc\Post;
use Boroughcc\Http\Controllers\Controller;
use 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'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create(Post $post)
{
$this->middleware('auth');
return view('posts.create', compact('post'));
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$input = Input::all();
$validation = Validator::make($input, Post::$rules);
// Get the uploaded file object
$img = Input::file('featured_image');
// Generate the necessary file details
$extension = pathinfo($img->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = date('Y-m-d-H:i:s') . '.' . $extension;
$path = 'img/posts/';
// Move the uploaded image to the specified path
// using the generated specified filename
$img->move($path, $filename);
// Save the post to the database
// using the path and filename use above
$post = Post::create(array(
'title' => Input::get('title'),
'body' => Input::get('body'),
'featured_image' => $path . $filename
));
return Redirect::route('journal')->with('message', 'Post created');
}
/**
* Display the specified resource.
*
* @param int $id
* @param int $slug_column
* @return Response
*/
public function show(Post $post)
{
//
$post->find($post);
return view('posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit(Post $post)
{
$post->find($post->slug);
$this->middleware('auth');
return view('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Post $post)
{
$this->middleware('auth');
$input = Request::except('_method','_token','featured_image');
$destinationPath = 'img/posts/';
if(Request::hasFile('featured_image')) {
$img = Request::file('featured_image');
$extension = pathinfo($img->getClientOriginalName(), PATHINFO_EXTENSION);
$filename = date('Y-m-d-H:i:s') . '.' . $extension;
$img->move($destinationPath, $filename);
$post->update(array(
'featured_image' => $destinationPath . $filename
));
} else {
// ...
}
$post->update($input);
return Redirect::route('posts.show', $post->getSlug() )->with('message', 'Post updated.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Post::destroy($id);
return Redirect::route('posts.index')->with('message', 'Post deleted.');
}
}
然后在我的Post模型中,我有以下设置:
<?php namespace Boroughcc;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model implements SluggableInterface {
use SoftDeletes;
// protected $fillable = ['deleted_at'];
protected $dates = ['deleted_at'];
protected $fillable = array('title', 'featured_image', 'body');
protected $guarded = ['_method'];
use SluggableTrait;
protected $sluggable = array(
'build_from' => 'title',
'save_to' => 'slug_column',
);
public static $rules = array(
'title' => 'required',
'featured_image' => 'required|image|mimes:jpeg,jpg,png,bmp,gif,svg'
);
}
这里可能至少有两个问题,但您应该查看@ceejayoz提到的日志
此处使用路由模型绑定,因此您的show
方法应如下所示:
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
public function destroy(Post $post)
{
$post->delete();
return Redirect::route('posts.index')->with('message', 'Post deleted.');
}
而不是
public function show(Post $post)
{
//
$post->find($post);
return view('posts.show', compact('post'));
}
因为现在你们正试图在雄辩模型上启动find方法来寻找雄辩模型。这行不通
您的destroy
方法也是如此
代码应该如下所示:
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
public function destroy(Post $post)
{
$post->delete();
return Redirect::route('posts.index')->with('message', 'Post deleted.');
}
编辑
此外,您还应该以这种方式为路由模型绑定返回一些内容+您应该绑定到post
,而不是post
(在您的路由中,您有{posts}
),因此正确的方法是:
Route::bind('posts', function($value){
return Post::where('slug_column', '=', $value)->firstOrFail();
});
在将结果分配给变量之前,您没有返回它。在Laravel的日志或Web服务器中,白色屏幕通常指示错误消息。如果您使用的是Homestead,请在虚拟机中选中/var/log/hhvm/error.log
。它不是完全白色/空白的,因为您可以看到它仍然呈现html等内容,显示仍然找不到该帖子的数据。我想知道我是否应该在路由中的绑定中去掉findOrFirst?这现在让我可以显示post数据并编辑post数据:Route::bind('posts',function($value){return post::whereSlugColumn($value)->first();})代码>但我无法在我的PostsController中删除带有此项的帖子:$post->delete()
@Mdunbavan您是否也在destroy
方法中将参数从$id
更改为Post$Post
,如我在回答中所示?是的,我也这样做了,这一定是路由绑定导致的it@Mdunbavan你确定你找到了摧毁方法吗?尝试在此方法开头添加:dd($post)
以确保此变量包含的内容以及此方法是否正在运行