Laravel INSERT查询添加不相关的列,使查询失败

时间:2014-03-16 15:04:05

标签: mysql laravel laravel-4 sql-update sql-insert

我遇到的错误是:

  

SQLSTATE [42S22]:找不到列:1054未知列'user_id'   '字段列表'(SQL:更新companies设置user_id = 3,updated_at   = 2014-03-16 14:59:56其中id = 1)

但我的代码不应该有UPDATE查询,这是一个新的INSERT!另外,如果你查看下面的表结构,它没有'user_id'字段,它正试图设置?

表格结构:

CREATE TABLE `companies` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `address_1` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `town` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `postcode` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `phone_number` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
  `approved` tinyint(1) NOT NULL,
  `logo_file_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `logo_file_size` int(11) DEFAULT NULL,
  `logo_content_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `logo_updated_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

看起来失败的INSERT查询:

public function postCreate(){
        $validator = Validator::make(Input::all(), User::$rules);

        if($validator->passes()){

            $user               = new User;
            $user->firstname    = Input::get('firstname');
            $user->surname      = Input::get('surname');
            $user->phone_number = Input::get('phone_number');
            $user->email        = Input::get('email');
            $user->password     = Hash::make(Input::get('password'));

            $user->save();

            $insertedId = $user->id;
            $user = User::where('id','=', $insertedId)->first();
            $user->attachRole(2);

            $company                =   new Company;
            $company->company_name  =   Input::get('company_name');
            $company->address_1     =   Input::get('address_1');
            $company->town          =   Input::get('town');
            $company->postcode      =   Input::get('postcode');
            $company->phone_number  =   Input::get('phone_number');
            $company->approved      =   '0';
            $company                =   Company::create(['logo' => Input::file('logo')]);

            $user->company()->save($company);

用户部分完美运行。然而,公司方面似乎失败了。我不知道为什么它会更新公司表,当它应该插入时。

对于徽标,我使用Stapler。当我检查错误的日志时,这是最后一个MYSQL命令 -

insert into `companies` (`logo_file_name`, `logo_file_size`, `logo_content_type`,

`logo_updated_at`, `updated_at`, `created_at`) values ('2012-12-07 16.31.42.png', '36952',

'image/png', '2014-03-16 14:59:56', '2014-03-16 14:59:56', '2014-03-16 14:59:56')

就像Laravel绕过第一条信息一样!

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先创建公司,然后保存。它只会创建一行,然后您可以将其与用户关联。

$company = Company::create(['logo' => Input::file('logo')]); 
$company->approved = '0'; 
$company->save();