vendredi 6 avril 2018

Laravel 5.1, passing my ID

I'm trying to pass an ID of a past and insert it into another database.

I got a page with shows posts like this

/posts/{id}

. From there on i want to add a comment section, so i got my form set up like this:

<form method="POST" action="/posts//comments">

(When i inspect the code, it inserts the right id and the "" spot.

inside my routes.php i got the folling route:

Route::post('/posts/{post}/comments', 'CommentsController@store');

and inside CommentsController.php i got the following

    public function store(Post $post)
{
    Comment::create([

        'body' => request('body'),
        'post_id' => $post->id 
    ]);

    return back();
}

When ever i try to add a comment i get this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null

When i change my code from CommentsController.php to this:

'post_id' => "2"

it works fine (but the comment will always be added at post with ID 2) I can't seem to find out why my id wont go trough.

Anny help with this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire