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

无法在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条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler