Mapping database with Laravel

0

I have to do a web project in php and I know general aspects of Laravel, so I would like to use it in the development, what stops me for the moment is to map the database that I have created in mysql with laravel, it is possible to do this? I was searching the internet but I can not find anything concrete, if you could help me with a guide or document.

Thanks in advance ..

Greetings _!

    
asked by Jonathan 12.07.2018 в 07:58
source

2 answers

2

You can use Eloquent Model Generator

Once the tool has been added to laravel, you can run the following to create the models:

Assuming you have a table called users

php artisan krlove:generate:model Usuario --table-name=usuarios

And that would generate the User class with their relationships, as long as the foreign keys are set, so you can realize.

As an alternative there is another tool called Reliese Laravel that arms all the models in a single command, but I have never used it.

Greetings!

    
answered by 12.07.2018 / 14:52
source
1

If you already have the database defined and you want to use these tables as models in Laravel simply configure your project to use the database you want, to do this modify the values in your environment variables:

/. env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mibasededatos
DB_USERNAME=el_usuario_de_tu_db
DB_PASSWORD=la_clave_de_tu_usuario

Then, create your models according to the tables in your database:

php artisan make:model Employee

and finally configure each model to connect to the correct table:

app / Employee.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model {

    // con lo siguiente configuras la tabla que utilizará el modelo
    protected $table = 'employees';

    // y con esto especificas el nombre de la llave primaria de la tabla
    protected $primaryKey = 'employee_id';

    // el resto del código.. por ejemplo relaciones con otros modelos
}
    
answered by 12.07.2018 в 08:10