【发布时间】:2015-07-22 17:59:01
【问题描述】:
我使用Laravel & jQuery 将insert 数据写入MySQL 表并取回新行的新ID。问题是我得到一个空对象作为响应。我也试过print_r我的控制器中的结果,但我得到了一个巨大的数组。
这是一些代码。
jQuery :
$.ajax({
url: "locations/new",
type: "get", //send it through get method
data:{name:name,description:description},
success: function(response) {
$data = $(response);
console.log($data);
},
error: function() {
console.log("Unable add")
}
});
控制器:
$name= Input::has('name') ? Input::get('name') : null;
$description= Input::has('description') ? Input::get('description') : null;
$add = Location::add($name, $description);
return $add;
和我的模特:
public static function add($name,$description){
$location_id = DB::table('locations')->insertGetId(
array('name' => $name, 'description' => $description)
);
Self::get($location_id);
}
public static function get($location_id){
$result = DB::table('locations')->where('location_id',$location_id)
->select(DB::raw('location_id,name'));
$result->get();
return $result;
}
我知道我在这里可能有一个错误。请除了你的答案,我想知道错误的原因。
提前谢谢..
【问题讨论】:
-
试试
console.log(response);看看会发生什么 -
我得到(一个空字符串)
-
只是扔在空中,也许我错了,但不应该是
return Self::get($location_id);吗? -
@OfirBaruch 我试过了,我得到“无法转换为字符串”错误
-
这很奇怪。让我们尝试破解此代码以对其进行调试。撤消我的建议并在
return $result之前添加var_dump($result)。输出是什么?
标签: php jquery mysql ajax laravel