DEV Community

Sunday Moses
Sunday Moses

Posted on

Creating and Migrating a Schema in Laravel

In this tutorial, we'll walk through the process of creating a schema with four fields (id, name, address, timestamp) and migrating it to be used in the database using Laravel's migration feature.

Step 1: Creating a Migration

  1. Open your command line interface.
  2. Run the following command to create a new migration file with a descriptive name: bash php artisan make:migration createuserstable
    1. This command will generate a new migration file in the database/migrations directory.

Step 2: Defining the Schema

  1. Open the newly created migration file (e.g., 2024_01_26_093217_create_users_table.php).
  2. Inside the up method, define the schema for the users table using the Schema facade. Add the required fields and their data types:

`use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('address')->nullable();
$table->timestamp('createdat')->useCurrent();
$table->timestamp('updatedat')->default(DB::raw('CURRENTTIMESTAMP ON UPDATE CURRENTTIMESTAMP'));
});
}`

Step 3: Running the Migration

  1. To run the migration and apply the schema to the database, execute the following command in the command line: bash php artisan migrate
    1. This will create the users table in the database with the defined fields.

Congrats! You've successfully created a schema for a users table with four fields and migrated it using Laravel. It's now ready to be utilized in your database operations!

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video πŸ“ΉοΈ

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay