Creating a new table through php artisan command
To create a new table through a PHP artisan command, you can use the make:migration
command followed by the name of the table you want to create.
Here is an example:
- Open your terminal and navigate to your Laravel project directory.
- Run the following command to create a new migration file:
php artisan make:migration create_table_name
Replace
table_name
with the name of the table you want to create.
For example, if you want to create a table called users
, you would run:
php artisan make:migration create_users_table
This will create a new file in the database/migrations
directory with a name like 2023_02_20_000000_create_users_table.php
.
- Open the newly created migration file and add the necessary code to create the table. For example:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration { /**
- Run the migrations.
- @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
This code creates a `users` table with several columns, including an `id` column, `name` column, `email` column, `email_verified_at` column, `password` column, and `remember_token` column.
4. Run the migration to create the table:
php artisan migrate
This will execute the migration and create the `users` table in your database.
That's it! You have now created a new table through a PHP artisan command.