1表单

nqwrtyyt  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(262)

我要存储用户的数据到2个表只有一个形式,这是它看起来像,表有1对1的关系
问题是我不知道如何为profiles表获取用户id,如何获取它?谢谢您

Form Register :

email       :
password    :
name        :
address     :

Users Table:

+------------+---------------+------+-----+---------+----------------+
| Field      | Type          | Null | Key | Default | Extra          |
+------------+---------------+------+-----+---------+----------------+
| id         | int(11)       | NO   | PRI | NULL    | auto_increment |
| email      | varchar(40)   | NO   |     | NULL    |                |
| password   | varchar(20)   | NO   |     | NULL    |                |
+------------+---------------+------+-----+---------+----------------+

Profiles Table:

+------------+---------------+------+-----+---------+----------------+
| Field      | Type          | Null | Key | Default | Extra          |
+------------+---------------+------+-----+---------+----------------+
| id        | int(11)        | NO   | PRI | NULL    | auto_increment |
| user_id   | int(11)        | NO   |     | NULL    |                |
| name      | varchar(40)    | NO   |     | NULL    |                |
| address   | varchar(70)    | NO   |     | NULL    |                |
+------------+---------------+------+-----+---------+----------------+
pgky5nke

pgky5nke1#

在控制器中
先插入用户以获取用户id
这是你的模型

public function addUsser()
    {
    $post = Input::All();

    $data = new Users;

    $data->email = $post['email'];
    $data->password = $post['password '];
    ...

    if($data->save()) {
        return Response::json(array('success' => true, 'last_insert_id' => $data->id), 200);
    }
}

在控制器中获取id

$inserted_id = Model::addUsser($data);

使用 $inserted_id 为了 Profiles Table

t9eec4r0

t9eec4r02#

使用内置的laravel方法:

$id = DB::table('users')->insertGetId([
    'email'      => $request->email, 
    'password'   => bcrypt($request->password), // or use a mutator
    'name'       => $request->name,
    'address'    => $request->address
]);

相关问题