Mates,我有一个问题是将记录保存到数据库中。 正如我理解错误,它似乎在表中不存在'id_purchase'。但确实如此。 我正在使用的代码如下:
<?php
class Purchproducts_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
$purchases = Purchproduct::where('id_hostel', '=', Session::get('id_hostel'))->get();
$response = array();
foreach($purchases as $purch){
$response[] = $purch->attributes;
}
return json_encode($response);
}
public function post_create()
{
$data = Input::json();
$purchase = Purchproduct::create(
array(
'id_hostel' => Session::get('id_hostel'),
'id_products' => $data->id_products,
'id_purchase,' => $data->id_purchases,
'qty' => $data->qty
)
);
return json_encode($purchase->attributes);
}
}
错误屏幕:
Unhandled Exception
Message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id_purchase,' in 'field list'
SQL: INSERT INTO `ehm_purchproducts` (`id_hostel`, `id_products`, `id_purchase,`, `qty`, `updated_at`, `created_at`) VALUES (?, ?, ?, ?, ?, ?)
Bindings: array (
0 => 1,
1 => '2',
2 => 2,
3 => '12',
4 => '2013-04-16 22:00:32',
5 => '2013-04-16 22:00:32',
)
Location:
/home/pablo/htdocs/insdev/laravel/laravel/database/connection.php on line 263
Stack Trace:
#0 /home/pablo/htdocs/insdev/laravel/laravel/database/connection.php(183): Laravel\Database\Connection->execute('INSERT INTO `eh...', Array)
#1 /home/pablo/htdocs/insdev/laravel/laravel/database/query.php(823): Laravel\Database\Connection->query('INSERT INTO `eh...', Array)
#2 [internal function]: Laravel\Database\Query->insert_get_id(Array, 'id')
#3 /home/pablo/htdocs/insdev/laravel/laravel/database/eloquent/query.php(283): call_user_func_array(Array, Array)
#4 /home/pablo/htdocs/insdev/laravel/laravel/database/eloquent/model.php(390): Laravel\Database\Eloquent\Query->__call('insert_get_id', Array)
#5 /home/pablo/htdocs/insdev/laravel/laravel/database/eloquent/model.php(390): Laravel\Database\Eloquent\Query->insert_get_id(Array, 'id')
#6 /home/pablo/htdocs/insdev/laravel/laravel/database/eloquent/model.php(205): Laravel\Database\Eloquent\Model->save()
#7 /home/pablo/htdocs/insdev/laravel/application/controllers/purchproducts.php(27): Laravel\Database\Eloquent\Model::create(Array)
#8 [internal function]: Purchproducts_Controller->post_create()
#9 /home/pablo/htdocs/insdev/laravel/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
#10 /home/pablo/htdocs/insdev/laravel/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('create', Array)
#11 /home/pablo/htdocs/insdev/laravel/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('create', Array)
#12 /home/pablo/htdocs/insdev/laravel/laravel/routing/route.php(153): Laravel\Routing\Controller::call('purchproducts@c...', Array)
#13 /home/pablo/htdocs/insdev/laravel/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#14 /home/pablo/htdocs/insdev/laravel/laravel/laravel.php(167): Laravel\Routing\Route->call()
#15 /home/pablo/htdocs/insdev/laravel/public/index.php(34): require('/home/pablo/htd...')
#16 {main}
表格结构:
CREATE TABLE IF NOT EXISTS `ehm_purchproducts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_hostel` int(11) NOT NULL,
`id_products` int(11) NOT NULL,
`id_purchase` int(11) NOT NULL,
`recibo` varchar(200) COLLATE utf8_spanish_ci NOT NULL,
`qty` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=1 ;
有什么想法吗?
答案 0 :(得分:14)
'id_purchase,'=&gt; $ DATA-&GT; id_purchases,
在那里是逗号,这就是为什么它抛出错误没有找到的字段。