Migrate my database from mysql to laravel

1

I am new with Laravel and I am seeing if there is a way to pass the database that I have in mysql, to laravel to be able to work with them.

I've already tried:

  • Modify the .env.
  • Modify config/database.php .
  • But what I find is that you can create a table in Laravel and pass it to mysql, but what I'm looking for is the opposite, or directly, working directly between mysql and laravel, without needing to make that intermediate point of:

    php artisan migrate
    

    Most of the information I find is to create your table and pass it to mysql with the php artisan migrate if it is in an empty database, but I have the data and I want to work on it.

    Or I can only create one class in php per table in database/migrations , with the fields that the database has and interact with each other.

    Greetings.

        
    asked by CodeNoob 18.11.2016 в 16:55
    source

    3 answers

    7

    I think you confuse several terms / technologies. You do not need to migrate a database to Laravel . Basically because MySQL is your data storage service and Laravel is a framework that can use any data storage service (among other things).

    In your question I infer that you have several problems and that I will try to solve them in this comment:

  • Use an existing database
  • and Migrations (from Laravel)
  • (1) Use a database

    Laravel allows you to access data in several ways.

    • Directly unused Laravel instantiating a PDO , or any other connector (this is not advisable).

    • Using DB

    • Using Eloquent

    If you want to use DB or Eloquent you just have to configure the file "config / database.php" with the data.

    With DB it's as simple as doing something that ( more info )

    $results = DB::select('select * from users where id = :id', ['id' => 1]);
    

    Eloquent is an ORM ( more info ) than allows you to interact with the database through models (classes). These models are associated with a table in your database. So that I could do something such that

    $all_users_of_my_database = App\Users::all();
    

    O

    App\Users::where('email', '[email protected]')->first()
    

    (2) Laravel Migrations

    Migrations in Laravel could be similar to having a version control system, on your database. For example, when you want to create a new table, instead of doing it directly from MySQL , what you would generate is a migration for that purpose. Migrations are also used to alter the fields of the tables and not have to do directly in MySQL : In one or several fields, add, delete, change the type, etc.

    Example:

    php artisan make:migration create_table_users --create=users --table=users
    Created Migration: 2016_11_18_164149_create_table_users
    

    Where you would then add the columns name, surname and email.

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
    
            $table->string('name');
            $table->string('surname');
            $table->string('email');
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('users');
    }
    

    After this, just by executing the Laravel migration, I would already create the table with the columns described.

    Summing up

    When your database already exists (because you implemented it in its day) it is not necessary that you create the migrations for what already exists, only for the new modifications (optionally). If you do not want to, you can make your application with Laravel on the one hand and work with manipulating your database directly with MySQL .

    Edited 2 : Please, before making modifications, ask the author of the answer. In general, changes do not seem bad to me except the first, which is not an "O" but a "Y". Thanks.

        
    answered by 18.11.2016 / 17:47
    source
    2

    If you want to be able to use Eloquent or the Query builder and not have to generate the models manually, you can use some packages that exist:

    link

    link

    This assuming that you have more than 10 or 20 tables and that the generation of your relationships is complex, otherwise I would create the models manually, it is not complicated and it does not take long either.

        
    answered by 18.11.2016 в 17:43
    0

    It's simple if you want to connect with modifications in the .env file but if you want to do the typical CRUD you have to create a model for each table and manage it by the controller. These videos served me well link

        
    answered by 22.11.2017 в 20:37