Вопрос

I'm implementing edition of post in my app. I came with this code:

Route::get('editpost', array('before' => 'auth', 'do' => function($id){
    $user = Auth::user();
    $view_post = Post::with('user')->find($id);

    return View::make('admin.editpost')
        ->with('user', $user)
        ->with('post', $view_post);
}));

And later I use the PUT route.

But when I try to edit testing post, I came up with Missing argument 1 for {closure}(). With a little search on Google I didn't seem to find solution. Deleted "$id" in function, and.. "Undefined variable".

Do you mind what cause a problem? Is that code correct?

Route::get('editpost', array('before' => 'auth', 'do' => function(){
    $user = Auth::user();
    $view_post = Post::with('user')->find($id);

    return View::make('admin.editpost')
        ->with('user', $user)
        ->with('post', $view_post);}));

Route::put('post/(:num)', array('before' => 'auth', 'do' => function($id) {
    $edit_post = array(
        'post_title'  => Input::get('post_title'),
        'post_body'   => Input::get('post_body'),
        'post_author' => Input::get('post_author')
    );

    $rules = array(
        'post_title'  => 'required|min:3|max:255',
        'post_body'   => 'required|min:10'
    );

    $validation = Validator::make($edit_post, $rules);
    if ($validation -> fails()){
        return Redirect::to('editpost')
            ->with('user',Auth::user())
            ->with_errors($validation)
            ->with_input();
    }

    $post = Post::with('user')->find($id);
    $post->post_title = $post_title;
    $post->post_body = $post_body;
    $post->post_author = $post_author;
    $post->save();


    //go to all post
    return Redirect::to('news')->with('success_message', true);})); 
Это было полезно?

Решение

I'm not familiar with L3, but I think you can do/verify that:

Fix your edit route

You forget the (:num) and the $id in function($id):

Route::get('editpost/(:num)', array('before' => 'auth', 'do' => function($id){
    $user = Auth::user();
    $view_post = Post::with('user')->find($id);

    return View::make('admin.editpost')
        ->with('user', $user)
        ->with('post', $view_post);}));

Check your Form

echo Form::open("post/{$post->id}", 'PUT');
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top