Laravel - 字段列表中的未知列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16048064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:42:44  来源:igfitidea点击:

Laravel - Unknown column in field list

phplaravellaravel-3

提问by Pablo

Mates, I'm havin a problem to save a record to the database. As I understand the error, it seems as it doesn't exist an 'id_purchase' in the table. But it does. The code I'm using is the following:

伙计们,我在将记录保存到数据库时遇到了问题。据我了解,该错误似乎在表中不存在“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);
    }  
}

The error screen:

错误画面:

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}

The table structure:

表结构:

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 ;

Any ideas?

有任何想法吗?

回答by hkusdaryanto

'id_purchase,' => $data->id_purchases,

'id_purchase,' => $data->id_purchases,

the is comma in there, that's why it throwing error no found field.

那里是逗号,这就是为什么它抛出错误未找到字段的原因。