Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RadicalActivity's avatar

How to work with an existing database?

Hello,

I'm quite new to laravel and I'm currently learning it to make some modifications to my website and add new features.

Is that possible with Laravel to connect to an existing database and do CRUD commands there? Assume that I have a fresh installation of Laravel, but have a quite huge database with data already in it.

Can you please explain how is this possible?

Thank you, RA

0 likes
16 replies
RachidLaasri's avatar
Level 41

Of course it is possible, just edit your .env file and you are ready to go.

1 like
RadicalActivity's avatar

Thanks for your reply. So I will not need to create any migrations or whatsoever?

RachidLaasri's avatar

No, nothing at all, edit your .env file, create your models and you are fine.

1 like
TonsOfLaz's avatar

Hi, I have a followup question to this. I was able to connect to my existing database fine on the server, but now I would like to develop locally. Where do I put an export of the existing database to be able to work with it locally? I am using Homestead for a local environment. Thanks in advance for any advice!

TonsOfLaz's avatar

For those future people doing the same thing: I was able to set up the database copy in my local environment with these steps:

  1. Export database from server in .sql format
  2. navigate to my homestead directory
  3. run vagrant ssh to get into the vagrant box
  4. navigate to my project root within the box
  5. run mysql -uhomestead -p (password: "secret")
  6. at the mysql prompt run CREATE DATABASE your_db_name;
  7. Control-C to exit mysql back to the vagrant prompt
  8. run mysql -uhomestead -p your_db_name < path/to/your/database_dump.sql

From here, you can log back into mysql (step 5) and run the command show databases; to see if your database is there. If it is, then run use your_db_name; to start running queries on the database to verify the data came over.

Good luck!

Additional Resources: http://stackoverflow.com/questions/25503327/laravel-homestead-mysql-default-credentials-and-database

2 likes
pdeschamps's avatar

TonsOfLaz what would you do next, would you run php artisan migrate? and since I am using a sql file will I have to run raw queries or will I be able to use eloquent?

ahuggins's avatar

@pdeschamps I think the easiest thing, is if you can access your db using a program like Sequel Pro or Navicat, then you should be able to import your .sql file. Running php artisan migrate isn't going to help you unless you have made migration files for your database structure.

Also @pdeschamps, you can use Eloquent, you may have to be more specific when joining if your existing DB structure is a little different than what Eloquent expects. The good thing though, is that Eloquent allows you to specify columns easily.

2 likes
pdeschamps's avatar

Thanks for the response, So @ahuggins basically there is no way to convert my existing mysql tables into a laravel 5 migration. I was able to import my sql file into a new db I created and access that data in my routes file with a raw sql querry. But when I created a new Article model, I was unable to create a new article in tinker and there is an articles table in my db. Also I keep ending up at this https://github.com/Xethron/migrations-generator -- Is this a way for me to set up migrations in my laravel 5 project?

ahuggins's avatar

I hadn't seen that generator before, that may work. It's definitely possible that there is a tool out there that can generate the migration files you would need, I am just unaware of it.

Not sure what is going on in your Article model/table. Hard to say without seeing the DB schema. what is your ID field called in the articles table? I think eloquent looks for 'id' so if you have 'article_id' you need to tell eloquent what to use.

Give that package a try and see if it works

pdeschamps's avatar

@ahuggins

So that generator is not going to work out for me, but I know what needs to be done, I need to set up a new db in my laravel project and seed the database from my existing database. The only problem is I'm not sure how to write the seed file, here is my user schema

| users | CREATE TABLE `users` (
  `user_id` mediumint(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) NOT NULL,
  `user_hashed_password` varchar(255) NOT NULL,
  `user_salt` varchar(255) NOT NULL,
  `user_email` varchar(100) NOT NULL,
  `user_first_name` varchar(100) NOT NULL,
  `user_last_name` varchar(100) DEFAULT NULL,
  `user_type` mediumint(4) NOT NULL DEFAULT '3',
  `user_login_count` int(11) NOT NULL DEFAULT '0',
  `user_verified` tinyint(1) NOT NULL DEFAULT '0',
  `tos_agreed` tinyint(1) NOT NULL DEFAULT '1',
  `user_verification_code` varchar(100) DEFAULT NULL,
  `user_creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_display_image` varchar(500) NOT NULL,
  `user_display_name` varchar(500) NOT NULL,
  `user_facebook_id` varchar(500) NOT NULL,
  PRIMARY KEY (`user_id`),
  KEY `user_email_index` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2031 DEFAULT CHARSET=latin1 |

If I can write the seed file properly then I can convert the table into my new laravel project as a migration, any help on how the seed file would look like or a good resource explaining how to do it would be helpful.

wickley's avatar

The term seeding generally refers to working with test data during development. If you're trying to work with an existing database and Laravel, you shouldn't need to write a "seed" file to do so.

You reference an existing Articles table in you current database. Is the primary key field on your Articles table labeled "id"? I notice on your users table, you use user_id, and so I'm guessing your Article table might have a primary key of article_id or similar. By convention, Laravel models look for the primary key to be labeled "id". You can override this by defining a $primaryKey property in the Model:

http://laravel.com/docs/5.1/eloquent#eloquent-model-conventions

Laravel models also use the $fillable or $guarded attribute to protect against mass-assignment vulnerabilities. You need to make sure those are set properly as well in order for the create method on the model to work:

http://laravel.com/docs/5.1/eloquent#mass-assignment

If you're looking to make your life a bit easier, this is the best CRUD generator that I've found so far that works with existing database tables:

https://github.com/mitulgolakiya/laravel-api-generator

If you read towards the bottom, it has a section titled "Generator from existing tables". I've done some testing with this generator and it works okay. It doesn't create migration files, but it does create a full basic working CRUD for the tables you run through the generator.

You have to go back and do some tweaking after running the command. Mainly you have to add the created_at and updated_at fields to each table or set the $timestamps property on the model to false. Again, these two timestamp fields are fields that Laravel's Eloquent models use, and look for, by convention.

In your case, you may have to go back and explicitly set the primary key property on the eloquent models after you run the generator.

Also, you'd obviously have to go back and manually alter the table field types and do any customization to the business logic. This generator is just to save you have having to write a bunch of boilerplate code.

socialflasher's avatar

Hi @wickley i was also having the same issue but i thought to give it a try :

but after installing it running the command:

getting this : artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider" Nothing to publish for tag [].

Process finished with exit code 0 at 19:34:55. Execution time: 393 ms.

am i missing some thing here or something else ? can you please help me thanks

marcellopato's avatar

Dear fellows,

I did config my database file so it can read my existing MySQL, but Tinker doesn't know my tables names. I´ve created a model named tipoProduto which is equal my table name, but Tinker looks for a table called tipo_produto. How can I fix this WITHOUT renaming my tables?

Erulezz's avatar

Set the correct table name in your model;

protected $table = 'tipoProduto';
2 likes

Please or to participate in this conversation.