JPC
JPC

Reputation: 5173

laravel pass parameter from one controller to another controller to view

I'm new to laravel : so I have 2 tables :

  1. users
  2. accounts

each time user log in they can go create account and put info their info in the page. I got 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();
    });

and

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');
        });

then I have my view to create.blade.php form information :

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

I'm trying to get view : of all value from the form and also email from my user table in view ... but I kept getting this error below : where do I from here. recommendation would be lovely, thanks

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, [email protected], 3, 2014-08-10 01:14:07, 2014-08-10 01:14:07))

Upvotes: 0

Views: 848

Answers (1)

Arjun Vs
Arjun Vs

Reputation: 372

@JPC,

Alright from what i understand about your query here, you are trying to insert a record into the accounts table and you are getting the error.

Because in your accounts table you have a field 'user_id' which is referencing a user record in the users table, you have to give a value for this field as well while you do an insert action in the accounts table or else there is a foreign key violation, thats what your error is all about.

You could do this either by adding a hidden field in the form itself by:

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

or by grabbing the logged in user through:

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

.Try this and let me know.

Upvotes: 1

Related Questions