THT DUYURU

 
 
Web Programming Section where Web programming languages are shared

Seçenekler

How to Create Custom Migration Directories in Laravel Projects

Gauloran - ait Kullanıcı Resmi (Avatar)
Kadim Üye
Üyelik tarihi:
07/2013
Nereden:
local
Mesajlar:
7.290
Konular:
1663
Teşekkür (Etti):
656
Teşekkür (Aldı):
1504
Ticaret:
(0) %
0
528
01-01-2021 03:39
#1
Create custom directory structure for migration files in your Laravel projects

This article describes how to create custom directory structure for migration files in your Laravel (6 or later) projects. You could organize your migration folder structure in a better way, especially if you have a lot of migration files.


Introduction

This tutorial describes how to specify custom folders for migration files in your Laravel (6 or later) projects.


What Is the Problem with Migration Directory

As you know, in Laravel, by default, all your migration files are placed in one directory called database/migrations/.
It's cool and nice, but if your project has grown, you may find that when this directory includes a lot of files, it's not easy to support them. In this case, the best solution is to create sub-directories (according to your needs or per version) and create (or put) your migrations files in specific sub-directory.
This is the standard way of how we use migration files in Laravel projects:




How to Implement Migration Sub-Directories

Let's try to improve this feature. For example, you may decide to separate migration files according to the version of your script, so the structure of your migration directory will look like this:




Now it looks much better, right? Instead of a long list of migration files.
So when you start a new version, you need to create a new directory, name it with the next version number and all migrations related to this version, create only in new directory.
Is this enough? Not really.

But default Laravel waiting you place migration files directly in database/migrations/ directory, not in sub-directories. So we just need to inform Laravel from where it has to take migration files. Let's do it.
We have to open AppServiceProvider.php file, and add in the boot() method following code, that will tell Laravel from where to take migration files.


Kod:
<?php
/*
|--------------------------------------------------------
|  Register custom migration paths
|  We specify sub-directory for each version, like:
|      - database/migrations/0.8.1/
|      - etc.
|  You may use DIRECTORY_SEPARATOR instead of /
|--------------------------------------------------------
*/
$this->loadMigrationsFrom([
  database_path().'/migrations/0.8.1',
  database_path().'/migrations/0.8.2',
  database_path().'/migrations/0.8.3',
]);
?>


Next time when you will need to add a new directory, just remember to update this list of default directories.
--------------------- more than you know
- Teşekkür etti.

Bookmarks


« Önceki Konu | Sonraki Konu »
Seçenekler