用户档案创建

时间:2015-04-17 13:16:52

标签: php laravel laravel-5

我试图在这里问每个人,一旦用户存储在数据库中,我将如何处理在配置文件表中添加一行。现在唯一正在发生的事情是在users表中创建了一个新行,这是预期的但我也想获取用户的id并添加一个新行并将其作为user_profiles表中的user_id字段

/**
 * 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位于基本存储库内,称为EloquentRepository,userRepository从该存储库扩展。

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

编辑:

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

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:-1)

有一个php函数 mysql_insert_id 来获取最后插入的id。通过使用该ID,您可以在user_profiles表中使用。

示例:

$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')";

这不是你想要的,但这会以其他方式帮助你。