DEV Community

Cover image for Why we use doctrine package in Laravel
Snehal
Snehal

Posted on

Why we use doctrine package in Laravel

Hello all programmer's here.

In this blog post I will describe you why we use doctrine library. This libraryis mostly used for modifying the column of database field with change() method to the to column within the migration file.

Let us see how to use it

Before modifying the column we first need to install it using the composer.

composer require doctrine/dbal
Enter fullscreen mode Exit fullscreen mode

Let me describe it with an example for better understanding:
You have created a migration file to add is_admin column in users table like below.

Schema::table('users', function (Blueprint $table) {
    $table->unsignedBigInteger('is_admin');
});
Enter fullscreen mode Exit fullscreen mode

But if you want to change the type of the is_admin column from unsignedBigInteger to boolean then you need to add another migration file and add change method to it.

change method allow us to modify type and attribute of an existing column refer to the below code.

Schema::table('users', function (Blueprint $table) {
$table-> boolean(is_admin)->change();
});
Enter fullscreen mode Exit fullscreen mode

This follwing types can be modified: and supported by doctrine library.

bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger, unsignedSmallInteger, and uuid
Enter fullscreen mode Exit fullscreen mode

We can also modify a column to be nullable as shown below:

Schema::table('users', function (Blueprint $table) {
    $table->boolean('is_admin')->nullable()->change();
});
Enter fullscreen mode Exit fullscreen mode

In this way we can modify the type of the field/column within a migration file.

Happy coding. 🦄 🦄
Thank you for reading. 🦁

Discussion (0)