我如何获取刚刚添加到store function()Larevel 6.0中的用户的ID

时间:2019-12-27 20:12:12

标签: php laravel postgresql

我在自己的控制器中注册了一个用户,我想要我刚刚添加的用户ID,因为我想在同一控制器和功能的另一个表中使用它 < / strong>

  • 下面,我将让我尝试但不起作用的代码让我想知道我该怎么做
  • 我想要这样的$usuario->fk_cliente_natural=$cliente_natural->id_cliente_natural;,但说$cliente_natural->id_cliente_natural为空,因为我没有得到最后一个ID

我的store function()的代码

   public function store(Request $request)
    {
        $cliente_natural=new Cliente_natural();
        $cliente_natural->primer_nombre=$request->primer_nombre;
        $cliente_natural->segundo_nombre=$request->segundo_nombre;
        $cliente_natural->primer_apellido=$request->primer_apellido;
        $cliente_natural->segundo_apellido=$request->segundo_apellido;
        $cliente_natural->cedula=$request->cedula;
        $cliente_natural->rif=$request->rif;
        $cliente_natural->numero_carnet=$request->numero_carnet;

        $usuario=new Usuario();
        $this->validate(request(), [
            'email' => 'required|email',
            'password' => 'required|confirmed|min:8',
        ]);
        $cliente_natural->save();
        $usuario->email=$request->email;
        $usuario->password=$request->password;
        $usuario->fk_rol=3;
        $usuario->fk_cliente_natural=$cliente_natural->id_cliente_natural;
        //$cliente_natural->fk_lugar=getMunicipio();

        $usuario->save();

        return view('home.home2');
    }

我的create function()的代码

public function create()
    {
        $cliente_natural = DB::select( DB::raw("SELECT id_cliente_natural,rif, numero_carnet,cedula,primer_nombre,
                                                segundo_nombre,primer_apellido,segundo_apellido,fk_lugar,password,
                                                email,fk_rol,id_usuario,fk_cliente_natural
                                            FROM cliente_natural,usuario"
                                        ));
        return view('auth.register')->with('cliente_natural',$cliente_natural);
    }

1 个答案:

答案 0 :(得分:0)

这就是我的解决方法

//$usuario->fk_cliente_natural = $cliente_natural->id_cliente_natural;

$checkT = DB::select(DB::raw("SELECT id_cliente_natural as id from cliente_natural WHERE rif = '$cliente_natural->rif' AND numero_carnet = '$cliente_natural->numero_carnet'"));
$id = $checkT[0]->id;

$usuario->fk_cliente_natural = $id;