About Laravel • Created in 2011 by Taylor Otwell • For developer productivity and happiness • Clean and readable code • Rapidly growing and helpful community • Thoroughly documented and readable core
// http://example.com/users Route::get('users', function() { // Get all users from DB. $users = User::all(); // Create the user list view. return View::make('users') ->with('users', $users); }); Route to closure
Route::controller('users', 'UsersController'); class UsersController extends BaseController { // http://example.com/users/profile/1 function getProfile($id) { return 'A user with ID: ' . $id; } } Route to RESTful controller
// Bind parameter to model. Route::model('user', 'User'); // http://example.com/profile/1 Route::get('profile/{user}', function(User $user) { // Do something with this user. }); Route Model Binding
@extends('layouts.master') @section('sidebar') @parent <p>This is appended to the master sidebar.</p> @stop @section('content') <p>This is my body content.</p> @stop Using Blade layouts
Artisan • CLI for Laravel • Driven by the Symfony/Console component • Used for common tasks like migrations • Provides helpers for code generating • Can be extended
# Starts up a PHP dev server (PHP 5.4). php artisan serve # Interact with your application. php artisan tinker # Generate a controller class. php artisan controller:make UsersController Some Artisan commands
// Get multiple records. $users = DB::table('users')->get(); // Get a single record. $user = DB::table('users')->where('name', 'Dries') ->first(); // Get records as a key/value array. $users = DB::table('users')->lists('name', 'id'); Query builder
// Just one simple rule. class User extends Eloquent {} // Get all users. $users = User::all(); // Find a specific user. $user = User::find(1); Basic Eloquent model
// The 'phones' table has a 'user_id' foreign key. class User extends Eloquent { public function phone() { return $this->hasOne('Phone'); } } // Get the user’s phone number. $phoneNumber = User::find(1)->phone->number; One to One
// The 'phones' table has a 'user_id' column. class User extends Eloquent { public function phones() { return $this->hasMany('Phone'); } } // Itterate over the user’s phones. foreach ($user->phones as $phone) {} One to Many
class Phone extends Eloquent { public function user() { return $this->belongsTo('User'); } } // Get the user to which this phone belongs to. $user = Phone::find(1)->user; Inverse relation
class User extends Eloquent { public function roles() { return $this->belongsToMany('Role'); } } foreach ($user->roles as $role) {} foreach ($role->users as $user) {} Many to Many
class Photo extends Eloquent { public function imageable() { return $this->morphTo(); } } class User extends Eloquent { public function photos() { return $this->morphMany('Photo', 'imageable'); } } Polymorphic
// Retrieve a user’s or a gallery’s photos. foreach ($user->photos as $photo) {} foreach ($gallery->photos as $photo) {} // Retrieving the owner. $imageable = Photo::find(1)->imageable; // $imageable can be User or Gallery. Polymorphic