我是laravel的新手:所以我有两张桌子:
每次用户登录时,他们都可以创建帐户并将信息放入页面中。 我有表:
public function up() {
Schema::create('users', function($table) {
$table->increments('id');
$table->string('email')->unique();
$table->boolean('remember_token');
$table->string('password');
$table->timestamps();
});
和
Schema::create('accounts', function($table) {
# AI, PK
$table->increments('id');
# created_at, updated_at columns
$table->timestamps();
# General data...
$table->integer('user_id')->unsigned();
$table->string('name_first');
$table->string('name_last');
$table->date('dob');
$table->string('address');
$table->string('phone');
$table->string('email')->unique();
$table->string('event');
# Define foreign keys...
$table->foreign('user_id')
->references('id')
->on('users');
});
然后我了解 create.blade.php 表单信息:
{{Form :: open(array(' url' =>' accounts')}}}
<div class="form-group">
{{ Form::label('name_first', 'Frist Name') }}
{{ Form::text('name_first', Input::old('name_first'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('name_last', 'Last Name') }}
{{ Form::text('name_last', Input::old('name_last'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('dob', 'DOB') }}
{{ Form::text('dob', Input::old('dob'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('phone', 'Phone Number') }}
{{ Form::text('phone', Input::old('phone'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('address', 'Mailing Address') }}
{{ Form::text('address', Input::old('address'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email', Input::old('email'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('event', 'Party/Event') }}
{{ Form::select('event', array('0' => 'Birthday', '1' => 'Baby Shower', '2' => 'Wedding', '3' => 'Anniversary'), Input::old('party'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Adding RSVP now!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
我试图获取视图:来自表单的所有值以及来自我的用户表中的电子邮件...但我在下面不断收到此错误:我从哪里开始。推荐会很可爱,谢谢
Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`authapp`.`accounts`, CONSTRAINT `accounts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: insert into `accounts` (`name_first`, `name_last`, `dob`, `address`, `phone`, `email`, `event`, `updated_at`, `created_at`) values (jea, las, 21, ;lsdkfj, ;fd, iamjeannie@gmail.com, 3, 2014-08-10 01:14:07, 2014-08-10 01:14:07))
答案 0 :(得分:1)
@JPC,
根据我对此处的查询的理解,您正试图将记录插入到帐户表中,并且您收到错误。
因为在您的帐户表中,您有一个字段'user_id',它引用了users表中的用户记录,所以当您在accounts表中执行插入操作时,您还必须为该字段赋值,否则是一个外键违规,这就是你的错误。
您可以通过以下方式在表单中添加隐藏字段来执行此操作:
{{ Form::hidden('user_id', $id) }}
或通过以下方式抓取登录用户:
Auth::user()->getId() or something
。试着这个,让我知道。