为什么laravel删除并显示方法在laravel 5中不起作用?

时间:2015-02-25 17:15:35

标签: php post laravel laravel-routing laravel-5

我在充足的资源以及他们的工作方式和使用宁静的东西构建rails应用程序方面相当充分我理解了这一切的前提以及它们是如何工作的,但是laravel实际上并不起作用。

我有一组帖子,其中包含每种方法的资源,例如:

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-title-page上显示任何数据并删除/ posts索引页面上的帖子。当我尝试删除它时说帖子在邮件中删除但它仍然存在,当我查看帖子显示网址时,它显示空白页面,数据应该是但是没有显示,当我访问该帖子上的/编辑网址时在田野里没有告诉我什么。基本上一切都是空白的。

在索引帖子页面上,我可以看到下面的内容是正确的:

enter image description here

在单一视图中,我得到了这个:

enter image description here

灰色栏中应该有一个标题,上面有一个带有正文的图片,但我什么都没看到。

我正在使用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资源路由,然后将slug列绑定为可访问的帖子单页,如下所示:

/// Posts ////


Route::resource('posts', 'PostsController');

Route::match(array('PATCH'), "/posts", array(
      'uses' => 'PostsController@update',
      'as' => 'posts.update'
));

Route::bind('post', function($value){
    $Model = Post::where('slug_column', '=', $value)->firstOrFail();
});

然后在我的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'
    );

}

所以我可以创建帖子,但我无法查看单个帖子,也无法正确删除帖子。

1 个答案:

答案 0 :(得分:3)

这里可能至少有两个问题,但你应该看看@ceejayoz提到的日志。

您在这里使用路由模型绑定,因此您的show方法应如下所示:

 public function show(Post $post)
 {
    return view('posts.show', compact('post'));
 }

而不是

public function show(Post $post)
{
    //
    $post->find($post);

    return view('posts.show', compact('post'));
}

因为现在你试图在Eloquent模型上启动find方法来找到Eloquent模型。这不起作用。

同样适用于您的destroy方法。

代码应如下所示:

public function destroy(Post $post)
{
    $post->delete();
    return Redirect::route('posts.index')->with('message', 'Post deleted.');
}

修改

此外你还应该以这种方式返回路由模型绑定的内容+你应该绑定到posts而不是post(在你的路由中你有{posts})所以正确的方式将是:

Route::bind('posts', function($value){
    return Post::where('slug_column', '=', $value)->firstOrFail();
});

在将结果分配给变量并且未将其返回之前。