Enter Data in a MySQL DB from Laravel

0

Controller

     class RegistroController extends Controller
{
    public function RegistroController(){
        return view('registra');
    }

    public function storeg(Requests $request)
    {
        \App\registro::RegistroController([
            'NombreEmpresa' => $requests['NombreEmpresa'],
            'Ruc'           => $request['Ruc'],
            'DireccionPrincipal' => $request['DireccionPrincipal'],
            'CorreoElectronico' => $request['CorreoElectronico'],
            'PaginaWeb'         => $request['PaginaWeb'],
            'RepresentanteLegal'  => $request['RepresentanteLegal'],
            'Registro'  => $request [date('l jS \of F Y h:i:s A')],
            'CodIdRep' => $request['CodIdRep'],
            'Telefono' =>  $request['Telefono'],
            'Celular'  =>  $request ['Celular'],
            'Estado'   =>  $request ['Estado'],
        ]);
        return "Empresa Registrada";
    }
}

Model

class registro extends Model
{
   protected $table = 'empresa';

   protected $fillable  = ['NombreEmpresa','Ruc','DireccionPrincipal','CorreoElectronico','PaginaWeb','RepresentanteLegal','Registro','CodIdRep','Telefono','Celular','Estado'];

   protected $PK = 'CodEmpresa';

}

View

<form action="rouete => registra" id="formulario" method="POST" class="smart-form client-form" novalidate="novalidate">
                                <header>
                                    Registrate GRATIS
                                </header>

                                <fieldset>
                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-user"></i>
                                            <input type="text" name="NombreEmpresa" placeholder="Nombre de la Empresa">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Nombre </b> </label>
                                    </section>

                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-envelope"></i>
                                            <input type="text" name="Ruc" placeholder="RUC">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Ruc</b> </label>
                                    </section>
                                    <div>

                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="DireccionPrincipal" placeholder="Direccion" id="Direccion">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Direccion</b> </label>
                                    </section>

                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="CorreoElectronico" placeholder="E-mail">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Correo Electronico</b> </label>
                                    </section>
                                    </div>
                                    <div>
                                        <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="PaginaWeb" placeholder="Pagina Web">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Pagina de la Empresa</b> </label>
                                    </section>
                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="RepresentanteLegal" placeholder="Representante">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Representante Legal</b> </label>
                                    </section>
                                    </div>
                                    <div>
                                        <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="DocIdRep" placeholder="DNI del Representante">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Documento del Representante</b> </label>
                                    </section>
                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="Telefono" placeholder="Telefono">
                                            <b class="tooltip tooltip-bottom-right">Ingrese el Telefono</b> </label>
                                    </section>
                                    </div>
                                    <section>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="text" name="Celular" placeholder="Celular">
                                            <b class="tooltip tooltip-bottom-right">Ingrese Celular</b> </label>
                                    </section>
                                </fieldset>


                                <footer>
                                    <button type="submit" class="btn btn-primary">
                                        Register
                                    </button>
                                </footer>

                                <div class="message">
                                    <i class="fa fa-check"></i>
                                    <p>
                                        <h3>Gracias porRegistrarse</h3>
                                    </p>
                                </div>
    </form>
    
asked by Samuel Rivas 16.04.2016 в 19:34
source

2 answers

1

Important that you validate that you made the necessary classes to save in the database, between them is Eloquent.

Additionally, the way to instantiate a new record the laravel can be seen in the following link Insert in Eloquent

<?php     
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent;
use App\registro;
 class RegistroController extends Controller
{
    public function RegistroController(){
        return view('registra');
    }

    public function storeg(Requests $request)
    {

        $registro = Registro::create(
        ['NombreEmpresa' => $requests['NombreEmpresa'],
            'Ruc'           => $request['Ruc'],
            'DireccionPrincipal' => $request['DireccionPrincipal'],
            'CorreoElectronico' => $request['CorreoElectronico'],
            'PaginaWeb'         => $request['PaginaWeb'],
            'RepresentanteLegal'  => $request['RepresentanteLegal'],
            'Registro'  => $request [date('l jS \of F Y h:i:s A')],
            'CodIdRep' => $request['CodIdRep'],
            'Telefono' =>  $request['Telefono'],
            'Celular'  =>  $request ['Celular'],
            'Estado'   =>  $request ['Estado']  
        ]);
        return $registro;
        //Le retorna en JSON el resultado de la insert
        //return "Empresa Registrada";
    }}

Now, the way you are keeping the record, apparently you are ONLY validating the client side (and it is important NOT to trust 100% of the content sent by the user.) You could include validation rules You can see more in the following link Validation en Laravel

Example:

public function storereg(Request $request)
{
    //Reglas de validation
    $this->validate($request, [
        'NombreEmpresa' => 'required|unique:posts|max:255',
        'DireccionPrincipal' => 'required',
    ]);

    // Si pasa la validacion continuar para insertar el registro
    //Resto del codigo
}

I hope this information will be helpful.

    
answered by 28.04.2016 в 22:18
1

Problem

I see that you have some errors in your function that is responsible for saving the data in your model. First of all:

$registro = \App\registro::RegistroController([ /* ... */

Your main problem is here, where you try to call a static function that does not exist in your model. The correct function is:

App\TuModelo::create(/* Los valores deseados para la creación de tu registro */);

It would be nice to add what you have written in your routes file, to know how you are handling the actions of your controllers.

Solution

Recall that there are multiple ways to save a new record in Laravel . In your case, we could see it in the following way:

public function storeg(Request $request) {

    /**
     * Creamos la instancia de la clase registro
     */
    $registro = new App\registro;

    /* Realizamos la asignación masiva */
    $registro->NombreEmpresa = $requests['NombreEmpresa'];
    $registro->Ruc = $request['Ruc'];
    $registro->DireccionPrincipal = $request['DireccionPrincipal'];
    /**
     * Se repite con los demás datos que desees asignar...
     */

    $registro->save();

    return "Empresa registrada";
}

The example I show you is a more object-oriented style, besides that Daniel Ferrans explained to you in an excellent way how to use the static method create in the models, I do not see the need to again how to do it that way.

Greetings.

    
answered by 27.08.2016 в 21:01