【问题标题】:laravel pass parameter from one controller to another controller to viewlaravel 将参数从一个控制器传递到另一个控制器以查看
【发布时间】:2014-08-10 01:18:41
【问题描述】:

我是 laravel 新手:所以我有 2 张桌子:

  1. 用户
  2. 帐户

每次用户登录时,他们都可以创建帐户并将信息放在页面中。 我得到了table

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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    @JPC,

    根据我对您的查询的理解,您正在尝试将记录插入到帐户表中,但您遇到了错误。

    因为在您的帐户表中,您有一个字段“user_id”引用用户表中的用户记录,所以当您在帐户表中执行插入操作时,您还必须为该字段指定一个值,否则在那里是外键违规,这就是你的错误。

    您可以通过以下方式在表单本身中添加一个隐藏字段来做到这一点:

    {{ Form::hidden('user_id', $id) }}
    

    或通过以下方式获取登录用户:

    Auth::user()->getId() or something
    

    。试试这个,让我知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多