This content originally appeared on DEV Community and was authored by Snehal
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
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');
});
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();
});
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
We can also modify a column to be nullable as shown below:
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->nullable()->change();
});
In this way we can modify the type of the field/column within a migration file.
Happy coding. ? ?
Thank you for reading. ?
This content originally appeared on DEV Community and was authored by Snehal
Snehal | Sciencx (2021-09-15T17:56:27+00:00) Why we use doctrine package in Laravel. Retrieved from https://www.scien.cx/2021/09/15/why-we-use-doctrine-package-in-laravel/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.