What is Laravel?

Laravel is the most popular PHP framework, known for elegant syntax, batteries-included features, and a rich ecosystem. It follows MVC architecture and embraces modern PHP practices.

Creating a Project

  composer create-project laravel/laravel myblog
cd myblog
php artisan serve
  

Visit http://localhost:8000.

Routing

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

Route::get('/', function () {
    return view('welcome');
});

Route::get('/users/{id}', function (string $id) {
    return "User ID: {$id}";
});
  

Controllers

  php artisan make:controller UserController
  
  namespace App\Http\Controllers;

class UserController extends Controller
{
    public function index()
    {
        return view('users.index', ['users' => User::all()]);
    }
}
  

Eloquent ORM

  // app/Models/User.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = ['name', 'email'];
}

// Usage
$user = User::create(['name' => 'Alice', 'email' => '[email protected]']);
$users = User::where('active', true)->get();
  

Blade Templates

  {{-- resources/views/users/index.blade.php --}}
@foreach ($users as $user)
    <p>{{ $user->name }} — {{ $user->email }}</p>
@endforeach
  

Database Migrations

  php artisan make:migration create_posts_table
php artisan migrate
  
  Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});
  

Artisan CLI

  php artisan make:model Post -mcr   # Model + migration + controller + resource
php artisan route:list
php artisan tinker                  # REPL for testing
  

Key Concepts to Master

Topic Purpose
Middleware Request filtering (auth, CORS)
Validation Form and API input validation
Queues Background job processing
Events & Listeners Decoupled application logic
Sanctum / Passport API authentication

Laravel accelerates development while teaching solid architectural patterns applicable beyond PHP.

Eloquent Relationships

  class User extends Model {
    public function posts() {
        return $this->hasMany(Post::class);
    }
}

class Post extends Model {
    public function user() {
        return $this->belongsTo(User::class);
    }
}

$user = User::with('posts')->find(1);
  

Form Validation

  $validated = $request->validate([
    'email' => 'required|email|unique:users',
    'password' => 'required|min:8|confirmed',
]);
  

Validation rules run automatically and return 422 responses for API routes.

Middleware Example

  php artisan make:middleware EnsureAdmin

// Redirect non-admins
public function handle(Request $request, Closure $next) {
    if (!$request->user()?->isAdmin()) {
        return redirect('/login');
    }
    return $next($request);
}
  

Register in bootstrap/app.php or app/Http/Kernel.php depending on Laravel version.

Environment and Config

Laravel reads .env at boot. Access values via config() helper, never env() outside config files:

  // config/mail.php
'from' => ['address' => env('MAIL_FROM_ADDRESS')],

// In application code
$from = config('mail.from.address');
  

Deployment Essentials

  composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --force
  

Common Pitfalls

  • Running php artisan serve in production — use PHP-FPM + Nginx instead.
  • Forgetting APP_KEY — encryption and signed URLs will fail.
  • N+1 queries — always use with() for relationships in list views.