簡體   English   中英

laravel將參數從一個控制器傳遞到另一個控制器以進行查看

[英]laravel pass parameter from one controller to another controller to view

我是laravel的新手,所以我有2個桌子:

  1. 用戶
  2. 賬戶

每次用戶登錄時,他們都可以創建帳戶並將其信息放入頁面中。 我有桌子

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>

我試圖從表單中獲取view的所有值,並從用戶表中獲取所有電子郵件的視圖...但我一直在下面收到此錯誤:我從哪里來。 推薦會很可愛,謝謝

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

@JPC,

從我對您的查詢的了解來看,您正在嘗試將記錄插入到account表中,並且遇到了錯誤。

因為在帳戶表中有一個“ user_id”字段,該字段引用了用戶表中的用戶記錄,所以在帳戶表中執行插入操作時,還必須為此字段提供一個值,否則會出現外來違反密鑰,那就是您的錯誤所在。

您可以通過以下方式在表單本身中添加一個隱藏字段來做到這一點:

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

或通過以下方式抓住登錄用戶:

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

試試這個,讓我知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM