douan2907 2018-01-11 16:15
浏览 109
已采纳

无法在laravel中提交表单数据

I am using laravel. Below is my form.

<h1>New Comment</h1>
<form method="post">

Comment: <input type="text" size="128" name="comment" value="{{old('body')}}" /></p>

    @if ($errors->has('body'))
    <p class="warning">Comment cannot be empty.</p>
    @endif

    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    {{ csrf_field() }}
</form>

My Route:

Route::post('/{id}', 'FilmsController@addComment')->name('addComment');

My controller:

public function addComment(Request $request)
{
    $request->validate([
        'body' => 'required',
    ]);

    $entry = new Comment();
    $entry->body = $request->body;
    $entry->save();

    return redirect('/');

My schema includes 'body'... $table->string('body');

I am seeing my warning message when I click submit. The same happens when trying to update as well as add a new comment.

  • 写回答

2条回答 默认 最新

  • douyun1852 2018-01-11 16:18
    关注

    Because the element's name is comment, change this:

    'body' => 'required',
    

    To:

    'comment' => 'required',
    

    And this:

    $entry->body = $request->body;
    

    To:

    $entry->comment = $request->comment;
    

    Also, add an action to the form, for example:

    <form method="post" action="/add-comment">
    

    And the route should be:

    Route::post('add-comment', 'FilmsController@addComment')->name('addComment');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?