为什么Laravel上的AJAX GET/POST会“;内部服务器错误”;当我尝试将数据用于数据库请求时


Why AJAX GET/POST on Laravel does "Internal Server Error" when I try to use data for database request?

我有一个特殊问题无法修复。。。

我拖动&在日历中删除一些事件。当我放下它们时,我会向我的控制器发送一个Ajax Post Request,它可以在数据库中插入一些我通过Ajax发送的带有事件名称的东西。

首先,在我的JS文件中有我的代码:

$("#someIDstuffHere").droppable({
    appendTo: "body",
    tolerance: "pointer",
    accept: ".external-event",
    drop: function(event, ui) {
        $(this).append($(ui.draggable));
        $.ajax({
            type: 'POST',
            url: 'events/add-cron-event',
            data: {
                "eventName": $(ui.draggable).text() // get the span text
            },
            success: function(data) {
                console.log("success");
            },
            error: function() {
            }
        });
    }
});

我的EventController.php

public function postAddCronEvent(){ // Laravel use RESTful, so function can have post/get + url like add-cron-event
    $data['input'] = Input::get('eventName'); // get the data from POST
    $cronState = CronState::where('name', '=', $data['input'])->take(1)->get(); // Laravel Eloquent ORM make me able to do that kind of request... 
    foreach ($cronState as $state) {
        var_dump($state);
    }
}

以及当我通过GET(而不是POST)使用var_dump时的结果(美化)。

object(
CronState
)#234 (
18
) {
['"table'":protected]=> string(
9
) '"cronState'"
['"connection'":protected]=> NULL
['"primaryKey'":protected]=> string(
2
) '"id'"
['"perPage'":protected]=> int(
15
)
['"incrementing'"]=> bool(
true
)
['"timestamps'"]=> bool(
true
)
['"attributes'":protected]=> array(
4
) {
['"id'"]=> string(
2
) '"14'"
['"name'"]=> string(
5
) '"test2'"
['"created_at'"]=> string(
19
) '"2014-04-16 12:35:35'"
['"updated_at'"]=> string(
19
) '"2014-04-16 12:35:35'" }
['"original'":protected]=> array(
4
) {
['"id'"]=> string(
2
) '"14'"
['"name'"]=> string(
5
) '"test2'"
['"created_at'"]=> string(
19
) '"2014-04-16 12:35:35'"
['"updated_at'"]=> string(
19
) '"2014-04-16 12:35:35'" }
['"relations'":protected]=> array(
0
) { }
['"hidden'":protected]=> array(
0
) { }
['"visible'":protected]=> array(
0
) { }
['"appends'":protected]=> array(
0
) { }
['"fillable'":protected]=> array(
0
) { }
['"guarded'":protected]=> array(
1
) {
[0]=> string(
1
) '"*'" }
['"touches'":protected]=> array(
0
) { }
['"with'":protected]=> array(
0
) { }
['"exists'"]=> bool(
true
)
['"softDelete'":protected]=> bool(
false
) }

当我这样做的时候,它是有效的。

    success: function(data) {
        console.log("success");
    }

在控制台中显示"成功".log…

但是,现在,当我这样做时:

foreach ($cronState as $state) {
    $cronEvent = CronEvent::find($state->id);
    $cronEvent->h = 8;
    $cronEvent->save();
}

我的Chromium控制台显示"内部服务器错误500"(GET和POST测试)。。。

我认为错误来自var_dumped数据。。。看起来很复杂。。。

谢谢你的帮助!

编辑到评论:

127.0.0.1-【2014年4月17日:10:52:48+0200】"POST/stage/public/events/add-cron event HTTP/1.1"500 1536"localhost/stage/public/events"Mozilla/5.0(X11;Linux i686)AppleWebKit/537.36(KHTML,类似Gecko)Ubuntu Chromium/330.1750.152 Chrome/330.1750.152Safari/537.36"

这是我在error.log或access.log中看到的唯一错误(此错误来自access.log…)

再次编辑:

$cronEvent=cronEvent::find($state->id);似乎有问题。。。

$state->id不起作用。。。

但如果我加上"21"或其他id,它就行了!

我试着用intval()转换为Int,但它再次不起作用。。。

已回答

我自动回答了一些调查。。。

这似乎是Eloquent的要求。。。

我犯了一个错误。我试图更新主键,但数据库中不存在"13"或"14"的主键。

但在纠正了这个错误后,我试图使用Eloquent,但它再也不起作用了。。。

所以,

我改为:

DB::table('cronEvent')
  ->where('state_id', $state->id)
  ->update(array('h' => 10));

我用了Query Builder而不是Eloquent,它很管用!

回答

我自动回答了一些调查。。。

这似乎是Eloquent的要求。。。

我犯了一个错误。我试图更新主键,但数据库中不存在"13"或"14"的主键。

但在纠正了这个错误后,我试图使用Eloquent,但它再也不起作用了。。。

所以,

我改为:

DB::table('cronEvent')
  ->where('state_id', $state->id)
  ->update(array('h' => 10));

我用了Query Builder而不是Eloquent,它很管用!