3. First app

Created Friday 31 October 2014

Sketching out

Requirements

Entities, relationships, attributes

The map of the app


Starting the app

Using the built-in development server

Writing the first routes

Route::get('/', function () {
return Redirect::to('cats');
});
Route::get('cats', function () {
return "All cats";
});
Route::get('cats/{id}', function ($id) {
return "Cat #$id";
})->where('id', '[0-9]+');

Catching the missing routes

App::missing(function ($exception) {
return Response::make("Page not found", 404);
});

Handling redirections

Returning views

<h2>About this site</h2>
There are over <?php echo $number_of_cats; ?> cats on this site!
Route::get('about', function () {
return View::make('about')->with('number_of_cats', 9000);
});

Preparing the database

Creating the Eloquent models

class Cat extends Eloquent {
protected $fillable = array ('name', 'date_of_birth', 'breed_id');
public function breed() {
return $this->belongsTo('Breed');
}
}
class Breed extends Eloquent {
public $timestamps = false;
public function cats() {
return $this->hasMany('Cat');
}
}

Building the database schema

public function up() {
Schema::create('cats', function($table) {
$table->increments('id');
$table->string('name');
$table->date('date_of_birth');
$table->integer('breed_id')->nullable();
$table->timestamps();
});
Schema::create('breeds', function ($table) {
$table->increments('id');
$table->string('name');
});
}
public function down() {
Schema::drop('cats');
Schema::drop('breeds');
}

Seeding the database

class BreedsTableSeeder extends Seeder {
public function run() {
DB::table('breeds')->insert(array (
array ('id' => 1, 'name' => "Domestic"),
array ('id' => 2, 'name' => "Persian"),
array ('id' => 3, 'name' => "Siamese"),
array ('id' => 4, 'name' => "Abyssinian"),
));
}
}

Mastering Blade


Creating a master view

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cats DB</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
<div class="page-header">
@yield('header')
</div>
@if(Session::has('message'))
<div class="alert alert-success">
****
</div>
@endif
@yield('content')
</div>
</body>
</html>
@extends('master')
@section('header')
<h2>About this site</h2>
@stop
@section('content')
**<p>There are over cats on this site!</p>**
@stop

Back to the routes

The overview page

Route::get('cats', function () {
$cats = Cat::all();
return View::make('cats.index')
->with('cats', $cats);
});
Route::get('cats/breeds/{name}', function ($name) {
$breed = Breed::whereName($name)->with('cats')->first();
return View::make('cats.index')
->with('breed', $breed)
->with('cats', $breed->cats);
});
@extends('master')
@section('header')
@if(isset ($breed))
****
@endif
<h2>
**All @if(isset($breed)) @endif Cats**
**<a href="" class="btn btn-primary pull-right">**
Add a new cat
</a>
</h2>
@stop
@section('content')
@foreach($cats as $cat)
<div class="cat">
**<a href="">**
**<strong> {} </strong> - {}**
</a>
</div>
@endforeach
@stop



Backlinks: