Question

I have a one page form, which data needs to be saved to 2 tables - Company and User. A Company has many users, a user has one company:

public function company()
    {
        return $this->hasOne('Company');
    }

The form is quite extensive, so I'll have to cut down on the code below!

The form is wrapped within:

{{ Form::open(array('url'=>'users/create', 'class'=>'form-horizontal', 'role'=>'form', 'files'=>'true')) }}

{{ Form::close() }}

The form uses the User's controller to save the data:

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

            $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        =       Company::create(['logo' => Input::file('logo')]);
            $company->approved      =   '0';

            $company->save();

My issue is, all of the $user variables save correctly. The only $company variables to save is the 'approved' and 'logo' variable. The company_name, address_1 etc... doesn't save to the database!

A sample of the form of a text field that does save is:

 <div class="form-group">
        {{ Form::label('surname', 'Surname', array('class'=>'col-sm-4 control-label')) }}
            <div class="col-sm-8">
               {{ Form::text('surname', null, array('class'=>'form-control', 'placeholder'=>'Surname')) }}
            </div>
 </div>

This one doesn't (but it passes the validation 'required'?!):

 <div class="form-group">
    {{ Form::label('Company Name', 'Company Name', array('class'=>'col-sm-4 control-label')) }}
    <div class="col-sm-8">
       {{ Form::text('company_name', null, array('class'=>'form-control', 'placeholder'=>'Company Name')) }}
    </div>
 </div>

I'm a little confused why 2/7 will save in the company table, but not the other 5! Any help would be greatly appreciated.

Was it helpful?

Solution

Here is your problem, you're reassigning $company model before saving it in this line:

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

which should be:

$company->logo = Input::file('logo');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top