How to create database schema (table) in laravel?

Member

by darion , in category: PHP Frameworks , a year ago

How to create database schema (table) in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , a year ago

@darion 

To create a database schema (table) in Laravel, follow these steps:

  1. Start by creating a new migration using the Laravel Artisan command make:migration. You can run the following command in your terminal: php artisan make:migration create_table_name --create=table_name Replace table_name with the name of your table. This command will create a new migration file in the database/migrations directory.
  2. Open the newly created migration file and edit the up method. This method is responsible for creating the database schema. Inside the up method, use the Schema facade to create the table using the create method. use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateTableNameTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('table_name', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('column_name'); // add more columns as required $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('table_name'); } } Customize the table schema by adding columns using the available column types and modifiers provided by Laravel. You can find all the available column types and modifiers in the Laravel documentation.
  3. Once you have defined your table schema, save the file and run the migration using the migrate command: php artisan migrate This command will create the database table based on the migration file you created.


That's it! Your database schema (table) is now created in Laravel. You can repeat these steps to create more tables as required for your application.