我想创建一个带有ajax和bootstrap模式的注释编辑器,但它不起作用。我试图基于此解决:CRUD operations。当我单击编辑按钮时,会出现Bootstrap模式,但是当我在那里写注释并单击更新按钮时,编辑器不会编辑它。有人可以帮帮我吗? 我的代码: web.php:
Route::get('/update','CommentsController@edit');
Route::post('/update','CommentsController@update');
CommentsController.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">×</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);
}
});
}
答案 0 :(得分:0)
你应该在路线中提到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
}
答案 1 :(得分:0)
你应该写这个
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!');
}