user3732216
user3732216

Reputation: 1589

User Profile Creation

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

Upvotes: 0

Views: 130

Answers (2)

Emeka Mbah
Emeka Mbah

Reputation: 17553

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

Upvotes: 3

Jagadeesh
Jagadeesh

Reputation: 754

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.

Upvotes: -1

Related Questions