我已成功发送数据,但在网络选项卡中,PUT 请求给我一个500 内部服务器错误。我使用 laravel 作为我的服务器端框架,并在我的 Controller 中像这样更新数据。
public function update($id)
{
$input = Input::all();
$player = Player::find($id);
$player->playerName = $input['playerName'];
$player->teamID = $input['teamID'];
$player->points = $input['points'];
$player->made_one = $input['made_one'];
$player->made_two = $input['made_two'];
$player->made_three= $input['made_three'];
$player->missed_one = $input['missed_one'];
$player->missed_two = $input['missed_two'];
$player->missed_three =$input['missed_three'];
$player->percentage = $input['percentage'];
$player->assists =$input['assists'];
$player->rebounds = ['rebounds'];
$player->steals = $input['steals'];
$player->turnovers = $input['turnovers'];
$player->fouls = $input['fouls'];
$player->feed = $input['feed'];
$player->save();
}
这与 backbone.js 配合得很好,那是我的 javascript 框架,我在更新模型时调用 .save()
方法,$input
上面的属性是我模型中的属性。
奇怪的是,如果我更新 less $input
属性并设置如下代码来执行此操作,我不会收到 500 错误。
public function update($id)
{
$input = Input::all();
$player = Player::find($id);
$player->playerName = $input['playerName'];
$player->teamID = $input['teamID'];
$player->points = $input['points'];
$player->save();
}
这两个函数仍然使用正确的数据更新我的数据库表。
这是 500 错误信息
{
"error":{
"type":"ErrorException",
"message":"preg_replace(): Parameter mismatch, pattern is a string while replacement is an array",
"file":"C:\\wamp\\www\\basketball-app-framework\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php",
"line":900
}
}
下面是我如何使用 backbone.js 保存它
var addThis = parseInt(this.model.get('assists')) + 1;
this.model.save({assists: addThis});
这是我保存点数的方式,我添加到 made_one
属性,同时也添加到 points
属性。
var addMake = parseInt(this.model.get('made_one')) + 1;
var addOne = parseInt(this.model.get('points')) + 1;
this.model.save({made_one: addMake, points: addOne});
最佳答案
这应该有效:
$player->rebounds = ['rebounds'];
到 $player->rebounds = $input['rebounds'];
关于javascript - 在 laravel 中发送多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21057047/