0

I'm trying to ask everyone here how I would handle adding a row to the profiles table once a user is stored in the database. Right now the only thing that is happening is a new row is created in the users table which is what is expected but I also want to take that id of the user and add a new row and put it as the user_id field in the user_profiles table.

/**
 * Save a new user.
 *
 * @param UserRequest $request
 * TODO: Find a way to add a profile to a user once the user is saved.
 *
 * @return Response
 */
public function store(UserRequest $request)
{
    $user = $this->userRepository->create($request->all());

    return redirect('users');
}

Create Method is inside of the base repository known as EloquentRepository from which the userRepository extends from.

/**
 * Create a new modal instance in the database.
 *
 * @param array $data Attributes to be saved for the user.
 *
 * @return mixed
 */
public function create(array $data)
{
    return $this->model->create($data);
}

EDIT:

Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->integer('user_role_id')->unsigned();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();

        $table->foreign('user_role_id')->references('id')->on('user_roles');
    });


Schema::create('user_profiles', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->text('bio')->nullable();
        $table->string('address')->nullable();
        $table->string('city')->nullable();
        $table->string('state')->nullable();
        $table->integer('postcode')->nullable();
        $table->string('country')->nullable();
        $table->string('phone')->nullable();
        $table->timestamp('birthday')->nullable();
        $table->string('facebook_username')->unique()->nullable();
        $table->string('twitter_username')->unique()->nullable();
        $table->string('google_plus_username')->unique()->nullable();
        $table->string('behance_username')->unique()->nullable();
        $table->string('pinterest_username')->unique()->nullable();
        $table->string('linkedin_username')->unique()->nullable();
        $table->string('github_username')->unique()->nullable();
        $table->string('youtube_username')->unique()->nullable();
        $table->string('instagram_username')->unique()->nullable();
        $table->string('external_link')->unique()->nullable();
        $table->timestamps();
        $table->softDeletes();

        $table->foreign('user_id')->references('id')->on('users');
    });
user3732216
  • 1,579
  • 8
  • 29
  • 54

2 Answers2

3
/**
 * Save a new user.
 *
 * @param UserRequest $request
 * TODO: Find a way to add a profile to a user once the user is saved.
 *
 * @return Response
 */
public function store(UserRequest $request)
{
    $user = $this->userRepository->create($request->all());

    //if you are doing what I think this should return the current created user - return $this->model->create($data); which is passed to $user 

    $user_id = $user->id;

    //do something with the user_id, example, assuming you have this profileRepository

    $this->profileRepository->create([
        'user_id'=>$user_id
    ]);

    return redirect('users');
}
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • This is pretty much what I'm wanting however $user keeps returning null when I dd($user) after the create statement of the userRepository. – user3732216 Apr 17 '15 at 14:09
  • Is id auto incremented in users table? – Emeka Mbah Apr 17 '15 at 14:12
  • I take that back I'm sorry I forgot I had originally overwritten the create function in the users repo so I reran it and I receive the user object back great but I get the SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`backstage`.`user_profiles`, CONSTRAINT `user_profiles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: insert into `user_profiles` (`updated_at`, `created_at`) values (2015-04-17 14:13:02, 2015-04-17 14:13:02)) – user3732216 Apr 17 '15 at 14:14
  • constraint issue. check your schema or your migrations. You can also edit your question to include your migration code. You can look at this for clues http://stackoverflow.com/questions/14063652/integrity-constraint-violation-1452-cannot-add-or-update-a-child-row – Emeka Mbah Apr 17 '15 at 14:32
  • You migration is okay. now looking at this `insert into user_profiles (updated_at, created_at) values (2015-04-17 14:13:02, 2015-04-17 14:13:02)) ` seems user_id is missing in the insert query, which was referenced. Do you have $fillable properties in your user_profiles model? if yes ensure user_id is in the array. Also ensure you pass user_id to create method `create([ 'user_id'=>$user_id ])` – Emeka Mbah Apr 17 '15 at 15:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75523/discussion-between-digitlimit-and-user3732216). – Emeka Mbah Apr 17 '15 at 15:10
  • I wanted to comment that I had left out the user_id field out of the fillable array inside my user profile model. – user3732216 Apr 17 '15 at 15:18
-1

There is a php function mysql_insert_id to get the last inserted id. By using that id you can use in the user_profiles table.

Example:

$query = "INSERT INTO test (value) VALUES ('test')";
mysql_query( $query ); 
$lastId = mysql_insert_id();// it will return the executed result of the query.

$query1 = "INSERT INTO test1 (user_id) VALUES ('$lastId')";

This is not exact what you want, but this will help you by some other way.

Jagadeesh
  • 734
  • 6
  • 26