Domanda

The error I am being presented with is:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' (SQL: update companies set user_id = 3, updated_at = 2014-03-16 14:59:56 where id = 1)

But my code shouldn't have an UPDATE query, it's a new INSERT! Also, if you look at the table structure below, there is no 'user_id' field, which it's trying to set?

The table structure:

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 ;

The INSERT query which appears to be failing:

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);

The user section works perfectly. However, the Company side of things appears to be failing. I'm not sure why it is updating the Companies' table, when it should be inserting.

For the logo, I am using Stapler. When I check the log on the error, this is the last MYSQL command -

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')

It's as if Laravel is bypassing the first pieces of information!

Any help would be greatly appreciated.

È stato utile?

Soluzione

Create the company first and then save it. It will create only one row, then you can associate it to the user.

$company = Company::create(['logo' => Input::file('logo')]); 
$company->approved = '0'; 
$company->save(); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top