How to Create Custom Migration Directories in Laravel Projects

Gauloran

Kıdemli Moderatör
7 Tem 2013
8,096
585
local
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:

laravel-migrations-old-structure.png



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:

laravel-migrations-structure.png



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:
[COLOR=Plum]<?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',
]);
?>
[/COLOR]


Next time when you will need to add a new directory, just remember to update this list of default directories.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.