Skip to content
Advertisement

How can i get the id of the user i just added in a store function() Larevel 6.0

I did a register of users in my own controller, i want the id of the user i just added because i want to use it in another table in that same controller and function

  • Below i will let the code of how i tried but doesn’t work i want to know how could i do that
  • I want something like this $usuario->fk_cliente_natural=$cliente_natural->id_cliente_natural; but sais that $cliente_natural->id_cliente_natural is null because i dont get the last id

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

Code of my 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);
    }

Advertisement

Answer

This is how i solve it

//$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;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement