我正在尝试将我的信息插入information table
,然后一步将information_id
插入advertise table
我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1452
Cannot add or update a child row: a foreign key constraint fails (`baroot_laravel`.`advertise`, CONSTRAINT `advertise_information_id_foreign` FOREIGN KEY (`information_id`) REFERENCES `information` (`id`)) (SQL: insert into `advertise` (`jobs_sub_category_id`, `area_id`, `description`, `updated_at`, `created_at`) values (, , , 2015-10-18 07:04:13, 2015-10-18 07:04:13))
这是我的代码:
Advertise_Model:
class Advertise_Model extends Model
{
protected $table = 'advertise';
protected $fillable = ['jensiat','information_id','wage_range_id','jobs_sub_category_id','jobs_type_id','age_range_id','experience_id','confirmed','description','city_id','area_id','academic_degree_id'];
public function Information()
{
return $this->hasOne('App\Information_Model','information_id','id');
}
}
Information_Model:
class Information_Model extends Model
{
protected $table = 'information';
protected $fillable = ['person_name','company_name','company_phone','person_mobile','location_id','person_id','email','website'];
}
我保存到db控制器:
public function AdminStore(Request $request)
{
Advertise_Model::create($request->all())->with(Information_Model::create($request)->all());
//Information_Model::create($request->all());
//Advertise_Model::create($request->all());
return view('Administrator.Advertise.index');
}
已编辑:
广告迁移:
Schema::create('advertise', function (Blueprint $table) {
$table->charset = 'utf8';
$table->collate = 'utf8_general_ci';
$table->increments('id');
$table->tinyInteger('jensiat');
$table->integer('information_id')->length(10)->unsigned();
$table->integer('wage_range_id')->length(10)->unsigned();
$table->integer('jobs_sub_category_id')->length(10)->unsigned();
$table->integer('jobs_type_id')->length(10)->unsigned();
$table->double('details_views')->length(10)->unsigned();
$table->integer('age_range_id')->length(10)->unsigned();
$table->integer('experience_id')->length(10)->unsigned();
$table->tinyInteger('confirmed');
$table->text('description');
$table->integer('admin_id')->length(10)->unsigned();
$table->integer('city_id')->length(10)->unsigned();
$table->integer('area_id')->length(10)->unsigned();
$table->double('view');
$table->integer('academic_degree_id')->length(10)->unsigned();
$table->rememberToken();
$table->timestamps();
});
Schema::table('advertise', function($table) {
$table->foreign('information_id')->references('id')->on('information');
});
信息迁移:
Schema::create('information', function (Blueprint $table) {
$table->charset = 'utf8';
$table->collate = 'utf8_general_ci';
$table->increments('id');
$table->string('person_name');
$table->string('company_name');
$table->string('company_phone');
$table->string('person_mobile');
$table->integer('location_id')->length(10)->unsigned();
$table->integer('person_id')->length(10)->unsigned();
$table->string('email');
$table->string('website');
$table->rememberToken();
$table->timestamps();
});
答案 0 :(得分:0)
你可以这样做。
// Insert Information
$InformationCreate = new Information_Model;
$InformationCreate->person_name = $Request->person_name;
$InformationCreate->company_name = $Request->company_name;
$InformationCreate->company_phone = $Request->company_phone;
$InformationCreate->person_mobile = $Request->person_mobile ;
$InformationCreate->location_id = $Request->location_id;
$InformationCreate->person_id = $Request->person_id;
$InformationCreate->email = $Request->person_id;
$InformationCreate->website = $Request->email;
$InformationCreate->save();
保存信息后,您可以使用$ InformationCreate-> id来获取在创建该信息记录期间使用的ID。
// Create Advertise
$AdvertiseCreate = new Advertise_Model;
$AdvertiseCreate->jensiat = $Request->jensiat;
$AdvertiseCreate->information_id = $InformationCreate->id;
当然,也许有一种更好的方法可以做到这一点,但它应该适合你。