使用 Laravel 5 和 Bootstrap 可编辑的内联编辑:不允许使用 405 方法

Inline editing with Laravel 5 and Bootstrap Editable: 405 Method Not Allowed

我的应用程序中的每个 post 都有一个评论表单和列表,我刚刚导入了 Bootstrap Editable 以便能够让用户无需重新加载页面就可以内联编辑他们的评论。

但我一直收到这个错误

MethodNotAllowedHttpException in RouteCollection.php line 219:

POST http://localhost/r2/public/posts/comment/update 405 (Method Not Allowed)

我假设这与我的评论路线有关,但我不知道是什么。

编辑:type: 'post' 添加到 ajaxOptions 后,我开始出现不同的错误

Creating default object from empty value

似乎 Input::get('commenter_comment') 没有返回任何东西。我猜这是错误的,因为这不是出现的 X-Editable 字段。

如何获取 X-Editable 字段?

路线

Route::get('{post}/comment', ['as' => 'comment', 'uses' => 'CommentController@index']);
Route::post('{post}/post_this_comment', 'CommentController@post_this_comment');
Route::get('{post}/recaptcha', 'CommentController@recaptcha');
Route::get('{post}/reply_comment', 'CommentController@reply_comment');
Route::post('{post}/per_page', 'CommentController@per_page');
Route::post('{post}/comment/update', 'CommentController@update');

Update() CommentController

中的方法
public function update() {
    $commentId = Input::get('pk');

    $newComment = Input::get('commenter_comment');

    $commentData = Comment::whereId($commentId)->first();

    $commentData->comment = $newComment;

    if($commentData->save())
        return Response::json(array('status'=>1));
    else
        return Response::json(array('status'=>0));
}

风景

<script type="text/javascript">
    $(document).ready(
        function(){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $('.testedit').editable({
                validate: function(value) {
                    if($.trim(value) == '')
                        return 'Value is required.';
                },
                type: 'textarea',
                url: 'comment',
                title: 'Edit Comment',
                placement: 'top',
                send:'always',
                ajaxOptions: {
                    dataType: 'json'
                }
            });
        }
    );
</script>

<p><a href="#" class="testedit" pk="{{ $each_comment->id }}">{!! $each_comment->comment !!}</a></p>

正如我提到的,当您使用不同的 HTTP 谓词时会抛出 MethodNotAllowedHttpException。我的意思是,在你的路线中,你用 post 动词声明更新路线:

Route::post('{post}/comment/update', 'CommentController@update');

然而,在你的 ajax 选项中,当未定义类型时,它会在 GET 中执行请求,因此你必须定义它:

ajaxOptions: type: 'post'

现在,另一个错误来了。您没有传递路由定义中预期的 post id,这就是您收到空 id 的原因,因此,从数据库返回一个空对象。因此,您必须将其传递为。但首先,让我们稍微更改每个评论的标记,以便通过 data-url 设置 post 的 url 和通过 data-pk 属性设置 pk:

<a 
href="#" 
class="testedit" 
data-pk="{{ $each_comment->id }}"
data-url="{!! url($post->id . '/comment/update') !!}">
    {!! $each_comment->comment !!}
</a>

现在,x-editable 将自动捕获 urlpk 值,而无需明确设置。您的最终代码应该像这样关闭:

$(document).ready(
        function(){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $('.testedit').editable({
                validate: function(value) {
                    if($.trim(value) == '')
                        return 'Value is required.';
                },
                type: 'textarea',
                title: 'Edit Comment',
                placement: 'top',
                send:'always',
                ajaxOptions: {
                    dataType: 'json',
                    type: 'post'
                }
            });
        }
    );

不要忘记在路由中定义的每个参数都可以注入到控制器的函数中。例如,您的路线正在定义一个 {post},它应该是要编辑的 post id:

Route::post('{post}/comment/update', 'CommentController@update'); 

因此,在您的 update() 中只需插入参数:

public function update(Request $request, $post) {
    //$post has the Id of post to be edited

    // catching pk id:
    $pk = $request->get('pk');

    // catching the new comment
    $comment = $request->get('value');

通过 laravel

使用 xeditable 的简单方法

HTML

<link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/jqueryui-editable/css/jqueryui-editable.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/jqueryui-editable/js/jqueryui-editable.min.js"></script> 

<div id="_token" class="hidden" data-token="{{ csrf_token() }}"></div>
<a href="#" id="industryName" data-type="text" data-pk="{{ $industry->id }}" data-title="Edit industry">{!! $industry->name !!}</a>

Javascript

    $(document).ready(function() {

     $.fn.editable.defaults.mode = 'inline';

       $.fn.editable.defaults.params = function (params) 
       {
        params._token = $("#_token").data("token");
        return params;
       };

     $('#industryName').editable({
                validate: function(value) {
                                            if($.trim(value) == '') 
                                                return 'Value is required.';
                                            },
                type: 'text',
                url:'/updateIndustry',   
                send:'always',
                ajaxOptions: {
                dataType: 'json'
                }

                } );
 } );

laravel路线

Route::post('/updateIndustry',  'SettingsController@updateIndustry');

laravel 控制器

public function updatePosition(Request $request)
    {
        $id= $request->pk;
        $position = $request->value;

        $count = DB::table('designations')->whereRAW("position LIKE '%".$position."%'")->where('id','!=',$id)->count();
        if($count)
            echo "Similar position exists.";

        else
        {
            DB::table('designations')->where('id',$id)->update(['position' => $position]);
            echo "1";
        }
   }