Answer the question
In order to leave comments, you need to log in
How to connect an already working database with migrations?
Framework - Lumen (same as Laravel, only less functionality).
DB - MySQL 5.5
Essence of a question: there is a ready DB with the filled tables. I can't find in the documentation how to work with existing tables in the database through migrations.
Description everywhere: install migrations -> create a table -> work with a file in the "migrations" folder.
Answer the question
In order to leave comments, you need to log in
If you really want to, you can write migrations that would create similar tables with all the fields and keys, then create a migrations table and populate it with your migrations. Here is an example from my database.
--
-- Структура таблицы `migrations`
--
CREATE TABLE IF NOT EXISTS `migrations` (
`migration` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`batch` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Дамп данных таблицы `migrations`
--
INSERT INTO `migrations` (`migration`, `batch`) VALUES
('2015_10_20_154943_create_sessions_table', 1),
('2015_10_21_063052_create_visits_table', 1),
('2015_10_21_165314_create_claims_table', 1),
('2015_10_21_171026_create_notifications_table', 1);
You can work with new ones without any problems,
create a migration and work with an existing table
Schema::table('users', function ($table) {
$table->string('email');
});
Migrations are a way to change a database. All examples show how to write migrations to create tables, but this is a more powerful tool. It allows you to make any changes to the base structure. This begs the question, what do you mean by "work with already existing tables through migrations"?
Just like with a non-existing one:
- If you want to have migrations to create your database, write them, if you don’t want, don’t write, just work with the database without migration
- If you want to modify the structure (once again I emphasize that migrations change the structure of the database, add, remove tables, fields in base, change their type and td) already available tables? Write migrations for these changes.
If you have a database and want to connect Laravel to them, then you don’t need migrations at all, since models, not migrations, are responsible for working with data in the database
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question