Track D · Tutorial 1

Laravel Fundamentals & Eloquent

Chapter 27 · Backend Specialization
~3 hours Intermediate PHP · Eloquent · Blade · Migrations

Overview

Laravel is the most popular PHP framework, known for its elegant syntax and powerful features. This tutorial covers the essentials: project setup, routing, Eloquent ORM, migrations, and Blade templating. You'll build a complete CRUD application.

Why this matters: Laravel is the gold standard for PHP development. Its expressive syntax and comprehensive ecosystem make it a top choice for web applications.

1. Laravel Setup

Install Laravel via Composer:

composer create-project laravel/laravel my-app cd my-app php artisan serve

Laravel's directory structure:

  • app/ – core application code (Models, Controllers)
  • database/ – migrations, seeders, factories
  • routes/ – web.php (web routes), api.php (API routes)
  • resources/views/ – Blade templates

2. Routing & Controllers

Define routes in routes/web.php.

// routes/web.php use App\Http\Controllers\ProductController; Route::get('/', function () { return view('welcome'); }); Route::resource('products', ProductController::class);

Controller

php artisan make:controller ProductController --resource
// ProductController.php public function index() { $products = Product::all(); return view('products.index', compact('products')); } public function store(Request $request) { $product = Product::create($request->all()); return redirect()->route('products.index'); }

3. Eloquent ORM

Eloquent is Laravel's ORM. Models represent database tables.

// Product.php class Product extends Model { protected $fillable = ['name', 'price', 'description']; }

Basic CRUD operations:

// Find all $products = Product::all(); // Find by id $product = Product::find(1); // Create $product = Product::create(['name' => 'Laptop', 'price' => 999]); // Update $product->update(['price' => 899]); // Delete $product->delete();

4. Migrations & Relationships

Create a migration

php artisan make:migration create_products_table
// database/migrations/xxx_create_products_table.php public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->decimal('price', 8, 2); $table->text('description')->nullable(); $table->timestamps(); }); }

Relationships

// User has many Products class User extends Model { public function products() { return $this->hasMany(Product::class); } } // Product belongs to User class Product extends Model { public function user() { return $this->belongsTo(User::class); } }

5. Blade Templating

Blade is Laravel's templating engine. Use .blade.php files.

{{-- products/index.blade.php --}} @extends('layouts.app') @section('content')

Products

    @foreach ($products as $product)
  • {{ $product->name }} - ${{ $product->price }}
  • @endforeach
@endsection

Quiz

Question 1

Which command creates a new Laravel project?

  • laravel new my-app
  • composer create-project laravel/laravel my-app
  • php artisan new my-app
  • npm create laravel
Show answer
B. composer create-project laravel/laravel my-app.

Question 2

Which method retrieves all records from an Eloquent model?

  • Model::get()
  • Model::all()
  • Model::find()
  • Model::fetch()
Show answer
B. Model::all().

Question 3

Which Laravel component is used for templating?

  • Twig
  • Blade
  • Mustache
  • EJS
Show answer
B. Blade.

Exercises

Exercise 1

Create a Category model and migration with fields: id, name, description. Run the migration.

Sample answer
php artisan make:model Category -m // Migration: add name, description columns // Model: protected $fillable = ['name', 'description']; php artisan migrate

Exercise 2

Add a route GET /categories that returns a view listing all categories.

Sample answer
// routes/web.php Route::get('/categories', function () { $categories = Category::all(); return view('categories.index', compact('categories')); }); // resources/views/categories/index.blade.php

Homework

Homework 1

Build a complete CRUD for Post with fields: title, content, author. Use Eloquent and Blade.

Sample outline
  • Model: Post (fillable: title, content, author)
  • Controller: PostController with index, create, store, show, edit, update, destroy
  • Routes: Route::resource('posts', PostController::class)
  • Views: posts/index.blade.php, create.blade.php, edit.blade.php, show.blade.php

Mini‑Project

Blog with Categories

Build a blog application with:

  • Posts: title, content, category_id, author
  • Categories: name, description
  • Relationship: Post belongs to Category
  • CRUD for both resources
  • Use Blade for the UI
Sample outline
  • Models: Post, Category
  • Migrations: posts (category_id foreign key), categories
  • Controllers: PostController, CategoryController
  • Routes: Route::resource for both
  • Views: Layout with navigation, post listing with category filter

Tutorial Summary

You learned the fundamentals of Laravel: project setup, routing, Eloquent ORM, migrations, relationships, and Blade templating. You built a CRUD application with a beautiful, expressive syntax.

Key takeaway: Laravel's convention‑over‑configuration approach makes development fast and enjoyable. Its ecosystem is unmatched in the PHP world.