app/model/manager.php class Manager extends Eloquent { public function projects() { return $this->hasMany(‘Project’); } } • File app/model/project.php class Manager extends Eloquent { public function manager() { return $this->belongsTo(‘Manager’); } }
class Movie extends Eloquent { public function scopePopular() { return $query->where(‘rating’,’>’,’5’); } } Movie::popular()->get(); // Dynamic scope class User extends Eloquent { public function scopeOfType($query, $type) { return $query->whereType($type); } } $users = User::ofType('member')->get();
public function getFullnameAttribute($value) { return $this->first_name . ‘ ‘ $this->last_name; } // fullname attribute become available echo $manager->fullname; // Mutator public function setFirstnameAttribute($value) { $this->attributes[‘name’] = strtolower($value); }
many queries. foreach (Project::all() as $project) { echo $project->manager->first_name; } // become this select * from projects; select * from manager where id = 1; select * from manager where id = 2; select * from manager where id = 3; select * from manager where id = 4; . . . . . . . . • Good ways, least queries. foreach (Project::with(‘manager’)->get() as $project) { echo $project->manager->first_name; } // become this select * from projects select * from manager where id in (1, 2, 3, 4, 5, …)
for your database structure (and content) • Paired with Schema Builder (manipulate db structure) • Run with Artisan http://laravel.com/docs/schema http://laravel.com/docs/migrations
artisan migrate:make create_posts_table // Setup migration schema (we also can setup it automatically) <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function(Blueprint $table) { $table->increments('id'); $table->string(‘title’)->nullable(); $table->text('body'); $table->timestamps(); $table->softDeletes(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('posts'); } }
@section(‘css’) <link rel=“stylesheet” type=“text/css” href=“mystyle.css”/> @endsection @section(‘javascript’) <script type=‘text/javascript’ src=“app.js”></script> @endsection @section(‘content’) This is the content for a particular page @endsection
allows you to declare the dependent libraries your project needs and it will install them in your project for you • Reusable component • Open/private source • > 5000 packages • Well tested & enterprise ready (some, actually :D) • http://getcomposer.org • http://packagist.org