【问题标题】:AJAX CRUD doesn't edit comments in Laravel 5.3AJAX CRUD 不会在 Laravel 5.3 中编辑评论
【发布时间】:2017-06-14 01:47:22
【问题描述】:

我想创建一个带有 ajax 和引导模式的评论编辑器,但它不起作用。我试图基于此解决:CRUD operations。当我单击编辑按钮时,会出现引导模式,但是当我在那里写评论并单击更新按钮时,编辑器不会编辑它。任何人都可以帮助我吗? 我的代码: web.php:

Route::get('/update','CommentsController@edit');
Route::post('/update','CommentsController@update');

评论控制器.php:

 public function edit($slug)
{
    $comment = Comment::whereSlug($slug)->firstOrFail();
}


public function update(CommentFormRequest $request, $slug)
{
    $comment = Comment::whereSlug($slug)->firstOrFail();
    $comment->comment = $request->get('comment');

    $comment->save();
    return redirect('/')->with('status', 'The comment '.$slug.' has been updated!');

}

home.blade.php:

<div class="container col-md-8 col-md-offset-2">
<div class="panel panel-default">
  <div class="panel-heading">
    <h2> Comments </h2>
  </div>
    @if ($comments->isEmpty())
      <p> There is no comment.</p>
    @else
      <table class="table">
        <thead>
          <tr>
            <th>ID</th>
            <th>Comment</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
          @foreach($comments as $comment)
            <tr>
            <td>{!! $comment->slug !!} </td>
            <td><a href="{!! action('CommentsController@show', $comment->slug) !!}">{!! $comment->comment !!} </a></td>
            <td>
              <button class="btn btn-warning" data-toggle="modal" data-target="#editModal" onclick="fun_edit('{{$comment -> slug}}')">Edit</button>
            </td>
            </tr>
          @endforeach
          </tbody>
        </table>
    @endif
</div>

 <input type="hidden" name="hidden_view" id="hidden_view" value="{{url('/')}}">

<!-- Edit Modal start -->
<div class="modal fade" id="editModal" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Edit</h4>
      </div>
      <div class="modal-body">
        <form action="{{ url('/update') }}" method="post">
          {{ csrf_field() }}
          <div class="form-group">
            <div class="form-group">
              <label for="edit_comment">Comment:</label>
              <input type="text" class="form-control" id="edit_comment" name="edit_comment">
            </div>

          <button type="submit" class="btn btn-default">Update</button>
          <input type="hidden" id="edit_id" name="edit_id">
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>

    </div>

  </div>
</div>
<!-- Edit code ends -->

还有 javascript:

function fun_edit(slug)
{
  var view_url = $("#hidden_view").val();
  $.ajax({
    url: view_url,
    type:"GET", 
    data: {"slug":slug}, 
    success: function(result){
      //console.log(result);
      $("#edit_id").val(result.slug);
      $("#edit_comment").val(result.comment);
    }
  });
}

【问题讨论】:

    标签: javascript jquery ajax laravel laravel-5


    【解决方案1】:

    您也应该在路由中提及slug

    Route::get('/update/{slug}', 'CommentsController@edit');
    Route::post('/update/{slug}', 'CommentsController@update');
    

    然后,您的控制器将有 slug 作为参数

    public function edit($slug){
     // rest of your logic
    }
    
    public function update(CommentFormRequest $request, $slug){
     // rest of your logic
    }
    

    【讨论】:

    • 感谢您的回答,我编辑了路线,但仍然无法正常工作。编辑评论后,我收到“评论字段为必填项”消息。
    • 您正在访问update 方法中的comment 字段。您的请求似乎没有 comment 值。如果发送的值完整,您是否检查了Developer Console - Network 部分中的请求?
    • 我在开发者控制台上看到了这个:GET homestead.app/%7Bslug%7D?slug=588c6385decf2 422 (Unprocessable Entity)
    • 请尝试使用此 URL homestead.app/slug/588c6385decf422。 Note 你必须遵循路由模式 /slug/{slug}
    【解决方案2】:

    你应该写这个

    Route::get('/update/{slug}', 'CommentsController@edit');
    Route::post('/update/{slug}', 'CommentsController@update');
    

    在你的控制器中你应该写

    public function edit($slug)
    {
        $comment = Comment::findOrFail($slug);
    }
    
    public function update(CommentFormRequest $request, $slug)
    {
        $comment = Comment::findOrFail($slug);
        $comment->comment = $request->get('comment');
        $comment->save();
        return redirect('/')->with('status', 'The comment '.$slug.' has been updated!');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      相关资源
      最近更新 更多