Add 0 on the left - PHP [duplicated]


My problem is as follows: When creating a new client, I need 6 digits to be stored in the database, starting with 000001 . The problem is that when I save it, in MySQL I keep only 1 and I need you to keep all 6 digits.

The field numero_tarjeta of the client table is the one that I need to save with the 6 digits.

This is the driver to add a new client:

public function create()
        $user = Auth::user();
        $nombre = $user->name;

        //obtenemos todas las categorias para cargar el combobox
        $cliente = Cliente::all();
        return view('cliente.crearCliente',['nombre'=>$nombre,'titulo'=>'Crear Cliente']);

    public function store(Request $request)
        ///validamos los campos enviados
        $validator =  Validator::make($request->all(), [
            'nombre' => 'required|min:3|regex:/^[\pL\s\-]+$/u',
            'apellido' => 'required|min:3|regex:/^[\pL\s\-]+$/u',
            'dni' => 'required|min:7|max:8',
            'fecha_nacimiento' => 'date_format:d/m/Y',
            'celular' => 'min:9',
            'email' => 'email',
            'cantidad_puntos' => 'required|numeric',
            'numero_tarjeta' => 'required'

        $numero_tarjeta = $request->input('numero_tarjeta');

        //si falla la validacion, redireccionamos con los errores
        if ($validator->fails())
            $errors = $validator->errors();
            $errors =  json_decode($errors);

            return response()->json([
                'success' => false,
                'message' => $errors
            ], 422);
        } else{
            //si no falla la validacion, se carga el cliente en la BD e informamos

            //cambiamos el formato de la fecha
            $fecha_nacimiento = $date = str_replace('/', '-', $request->input('fecha_nacimiento'));
            $request->merge(array('fecha_nacimiento' => date("Y-m-d", strtotime($fecha_nacimiento))));
            //$request->merge(array('fecha_nacimiento' => date("Y-m-d", strtotime($request->input('fecha_nacimiento')))));

            $numero_tarjeta = LPAD('numero_tarjeta',6,'0');

            //guardar los datos
            $cliente = New Cliente();

            return response()->json([
                'success' => true,
                'message' => 'record updated'
            ], 200);

And my client table is the following:

Schema::create('clientes', function (Blueprint $table) {
asked by Pablo Rivera Madrazo 18.03.2017 в 22:14

2 answers


numero_de_tarjeta is an entire field that you need to display as a string. Something like

$numero_de_tarjeta_texto = str_pad($number,6,"0",STR_PAD_LEFT); 
answered by 18.03.2017 / 22:38

If the problem as you describe you have it in the field of your table clients of the database then you should only alter the field of the table, assuming it is MySQL:

ALTER TABLE 'clientes' CHANGE 'numero_tarjeta ' 'numero_tarjeta ' INT(6) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT;

Otherwise, if you do not want to modify the field, you can create a mutator from the card_number field in the model like this:

public function getNumeroTarjetaAttribute() {
  return sprintf("%05d", $this->attributes['numero_tarjeta']);
answered by 03.09.2017 в 12:04