1个具有关系

时间:2018-05-04 03:18:45

标签: php mysql database laravel lumen

我要将用户的数据存储到只有1个表单的2个表中,这是它的样子,这些表有1比1的关系

问题是我不知道如何获取配置文件表的user_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    |                |
+------------+---------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:0)

在控制器中

首先插入用户以获取user_id

这是你的模特

sciChartSurface = SCIChartSurface()
view.addSubview(sciChartSurface)
sciChartSurface.translatesAutoresizingMaskIntoConstraints = false
sciChartSurface.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
sciChartSurface.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
sciChartSurface.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
sciChartSurface.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

let xAxis = SCIDateTimeAxis()
xAxis.visibleRange = SCIDateRange(dateMin: Date().addingTimeInterval(-86400), max: Date().addingTimeInterval(2 * 86400))
xAxis.growBy = SCIDoubleRange(min: SCIGeneric(0.1), max: SCIGeneric(0.1))
xAxis.textFormatting = "MMM d"
sciChartSurface?.xAxes.add(xAxis)

let yAxis = SCINumericAxis()
yAxis.textFormatting = "%.1f"
yAxis.axisTitle = "Temperature"
sciChartSurface?.yAxes.add(yAxis)

lineDataSeries = SCIXyDataSeries(xType: .dateTime, yType: .double)
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(-86400)), y: SCIGeneric(28))
lineDataSeries.appendX(SCIGeneric(Date()), y: SCIGeneric(30))
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(86400)), y: SCIGeneric(26))
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(2 * 86400)), y: SCIGeneric(28))

let series = SCIFastLineRenderableSeries()
series.dataSeries = lineDataSeries
series.strokeStyle = SCISolidPenStyle(colorCode: 0xFF279B27, withThickness: 1.0)
sciChartSurface.renderableSeries.add(series)

在控制器中获取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);
    }
}

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

答案 1 :(得分:0)

使用内置Laravel method

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