Docs menu

Learning Logs · · 2 min read

Learning Laravel: installation to your first CRUD

Laravel from scratch: Composer install, routes + Blade, migrations + Eloquent, server validation, and the daily artisan workflow — until your first product CRUD runs.

LaravelPHPEloquentBlade

Laravel is the fastest way to build a full-stack web app in PHP — routing, database, auth, and templating come ready with clear conventions. This tutorial takes you from installation to your first CRUD.

1. Installation

Prerequisites: PHP 8.2+ and Composer. Check first:

php -v && composer -V

Create a new project and run it:

composer create-project laravel/laravel shop-app
cd shop-app
php artisan serve        # open http://localhost:8000

Modern Laravel defaults to SQLite (database/database.sqlite) — zero config, migrations work immediately. Switch to MySQL/Postgres later via .env when you need to.

2. First route + view

// routes/web.php
use Illuminate\Support\Facades\Route;

Route::get('/products', function () {
    $products = [
        ['name' => 'Gayo Coffee', 'price' => 85000],
        ['name' => 'Toraja Coffee', 'price' => 95000],
    ];
    return view('products.index', ['products' => $products]);
});
{{-- resources/views/products/index.blade.php --}}
<h1>Products</h1>
<ul>
  @foreach ($products as $p)
    <li>{{ $p['name'] }} — {{ number_format($p['price']) }}</li>
  @endforeach
</ul>

Blade escapes {{ }} automatically — XSS-safe with zero extra effort.

3. Database: migrations + Eloquent

One command creates the model, migration, and controller at once:

php artisan make:model Product -mrc
// database/migrations/xxxx_create_products_table.php
public function up(): void
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->unsignedInteger('price');
        $table->timestamps();
    });
}
php artisan migrate

Then CRUD reads like plain language:

// app/Http/Controllers/ProductController.php
public function index()
{
    return view('products.index', ['products' => Product::latest()->get()]);
}

public function store(Request $request)
{
    $data = $request->validate([
        'name'  => 'required|string|max:100',
        'price' => 'required|integer|min:0',
    ]);
    Product::create($data);         // add $fillable on the model
    return redirect('/products');
}

Register it as a resource route: Route::resource('products', ProductController::class);

4. Daily workflow

  • php artisan migrate:fresh --seed — reset the database + seed sample data during development.
  • php artisan tinker — a REPL to try Eloquent queries directly.
  • php artisan route:list — see every registered route.

Tips from experience

  • Always validate on the server (like the $request->validate() example) — never trust input from the browser.
  • Follow Laravel’s naming conventions (model Product → table products) — fighting the conventions means fighting the framework.
  • For my clients whose hosting already runs PHP, Laravel is often the most pragmatic choice — deployment is simple and PHP developers are easy to find. The best framework is the one your team can maintain.

Want something like this built for your business?