[英]Form data not being saved
I have a one page form, which data needs to be saved to 2 tables - Company and User. 我有一个页面表单,该数据需要保存到2个表中-公司和用户。 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. 我的问题是,所有$ user变量都正确保存。 The only $company variables to save is the 'approved' and 'logo' variable.
唯一要保存的$ company变量是'approved'和'logo'变量。 The company_name, address_1 etc... doesn't save to the database!
公司名称,地址_1等...不会保存到数据库中!
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! 我有些困惑,为什么2/7将保存在company表中,而不保存其他5个! Any help would be greatly appreciated.
任何帮助将不胜感激。
Here is your problem, you're reassigning $company model before saving it in this line: 这是您的问题,在将其保存在此行之前,您需要重新分配$ company模型:
$company = Company::create(['logo' => Input::file('logo')]);
which should be: 应该是:
$company->logo = Input::file('logo');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.