DEV Community

Salma
Salma

Posted on

1 1 1 1 1

Laravel Blueprint To built fast laravel app

Hey developers! Today, I want to share a personal story about how Laravel's Blueprint significantly streamlined my development process, especially when it came to handling migrations, models, and factories. It's a game-changer, and here's why.

Rapid Database Setup with Migrations
Blueprint in Laravel isn't just about creating tables; it's about setting up your entire database environment swiftly and efficiently. With a few lines of code, I was able to define my tables, columns, and relationships. No more manual SQL! Here’s a quick snippet:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->decimal('price', 8, 2);
    $table->timestamps();
});
Enter fullscreen mode Exit fullscreen mode

Effortless Model Creation
Once the migrations were in place, setting up Eloquent models was straightforward. Laravel's artisan command php artisan make:model Product instantly created a model based on my migration. This tight integration ensures consistency across your database and application logic.

Run and Relax
With all pieces in place – migrations, models, and factories – deploying changes and testing them became a matter of running a few commands. php artisan migrate, php artisan db:seed, and I was ready to test my application with a rich, well-structured database.

Absolutely! If you're starting a new project in Laravel and want to leverage Blueprint to streamline your development process, here's a step-by-step guide to get you up and running:

Step 1: Install Laravel

First, you need to create a new Laravel project. If you haven't already, you can do this using Composer:

composer create-project --prefer-dist laravel/laravel your-project-name
Enter fullscreen mode Exit fullscreen mode

Navigate into your project directory:

cd your-project-name
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Your Database

Before you start using Blueprint, make sure your database is configured. Open the .env file in your project root and update the database configuration settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Blueprint

Blueprint is a package that helps you generate migrations, models, controllers, and more from a single configuration file. You can install it via Composer:

composer require --dev laravel-shift/blueprint
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Blueprint Configuration File

Blueprint uses a draft.yaml file to define your database schema and other configurations. Create this file in the root of your project:

touch draft.yaml
Enter fullscreen mode Exit fullscreen mode

Step 5: Define Your Schema in draft.yaml

Open the draft.yaml file and define your database schema. Here’s an example of how you might define a products table:

models:
  Product:
    name: string
    price: decimal:8,2
    timestamps: ~

controllers:
  Product:
    resource: web
Enter fullscreen mode Exit fullscreen mode

This configuration will generate a migration, model, and controller for the Product entity.

Step 6: Generate Migrations, Models, and Controllers

Once your draft.yaml file is ready, you can use Blueprint to generate the necessary files. Run the following command:

php artisan blueprint:build
Enter fullscreen mode Exit fullscreen mode

This command will generate:

  • A migration file for the products table.
  • An Eloquent model for the Product entity.
  • A controller for handling web requests related to products.

Step 7: Run Migrations

Now that your migration files are generated, you can run them to create the tables in your database:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 8: Seed Your Database (Optional)

If you want to populate your database with test data, you can create seeders. Blueprint can also help with this. Add seeders to your draft.yaml:

seeders:
  Product:
    - name: 'Sample Product'
      price: 19.99
Enter fullscreen mode Exit fullscreen mode

Then, generate the seeder:

php artisan blueprint:build
Enter fullscreen mode Exit fullscreen mode

Finally, run the seeder:

php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

Step 9: Test Your Application

With everything set up, you can now test your application. Start the Laravel development server:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Visit your application in the browser and verify that everything is working as expected.

Step 10: Iterate and Expand

As your project grows, you can continue to update your draft.yaml file and use Blueprint to generate new migrations, models, controllers, and more. This iterative process ensures that your database and application logic remain consistent and up-to-date.

Conclusion
Blueprint has transformed my Laravel development process. It's not just about building things right; it's about building them fast and with precision. Whether you're setting up a new project or maintaining an existing one, integrating migrations, models, and factories via Blueprint can save hours of work and potential headaches.

If you've been manually handling database changes or struggling with data consistency, give Blueprint a try – it might just change your development life!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay