Learn Laravel in 1 Week: Devan Singaram
Learn Laravel in 1 Week: Devan Singaram
Learn Laravel in 1 Week: Devan Singaram
Devan Singaram
Learn laravel in 1 week
A social initiative by TribeHired.com for craftsmen
Devan Singaram
This book is for sale at http://leanpub.com/leanlaravel
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
©2015 TribeHired.com
Tweet This Book!
Please help Devan Singaram by spreading the word about this book on Twitter!
The suggested tweet for this book is:
I just downloaded a draft copy of lean laravel in 1 week
The suggested hashtag for this book is #tribehired.
Find out what other people are saying about the book by clicking on this link to search for this
hashtag on Twitter:
https://twitter.com/search?q=#tribehired
Contents
Chapter One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
M-V-C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Blades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Eloquent ORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Artisan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Chapter Three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Setting up environment on the local server . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter Four . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Chapter Five . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Blades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter Six . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Chapter Seven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Chapter Eight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Schema Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Chapter Nine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Eloquent ORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter Ten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter Eleven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter Twelve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
CONTENTS
CRUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter Thirteen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Deploying live . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter Fourteen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Deploy DB live . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter Fifteen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Further customizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Chapter One
Our goal is to build a to-do list application in a typical work environment. We will learn the concepts
that laravel uses, install the development environment, and deploy live.
Chapter 2 has a quick introduction to Model-View-Controller.
Chapter Two
Basic concepts
M-V-C.
Laravel uses Model View Controller to make programming pleasant.
Model.
This layer will handle retrieving data from a database and store it within a class.
View.
This consists of the html layer where your design of your site resides.
Controller.
All your logic to manipulate data resides here, and present it to the view.
Blades
Blade is a templating engine, that makes it easy to add dynamic data to html.
Eloquent ORM
It’s a simple way to work with the Database in the model layer.
Artisan
A command line interface for laravel. To create controllers, etc.
Chapter Three
Setting up environment on the local server
Step 1. Go to http://sourceforge.net/projects/laragon/, click download and execute
Step 2. Download php ide, http://www.sublimetext.com/3
Step 3: Set up a virtual host
Go to menu apache2 httpd.conf on laragon
Add this to the end of file.
1 <VirtualHost *:4000>
2 ServerName doesnotmatter
3 DocumentRoot "C:/lamp/www/myapp/public"
4 ServerAdmin bruno.skvorc@sitepoint.com
5 <Directory "C:/lamp/www/myapp/public">
6 Options Indexes FollowSymLinks
7 AllowOverride All
8 Require all granted
9 Order allow,deny
10 Allow from all
11 </Directory>
12 </VirtualHost>
1 Listen 0.0.0.0:4000
1 cd www
2 composer create-project laravel/laravel myapp ~4.2.0 --prefer-dist
Chapter Three 4
1 @extends('layouts.main')
2 @section('content')
3 hello world
4 @stop
1 Route::get('/', function()
2 {
3 return View::make('todos.index');
4 });
5
6 Route::get('/todos', function()
7 {
8 return View::make('todos.index');
9 });
10
11 Route::get('/todos/{id}', function($id)
12 {
13 return View::make('todos.show')->withId($id);
14 });
Go to views folder, and create todos folder, then create index.blade.php, enter the following:
1 hello world!
Chapter Five
Templates
Go to views layouts main.blade.php
Enter the following:
1 <!doctype html>
2 <html class="no-js" lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 <title>Foundation | Welcome</title>
7 <link rel="stylesheet" href="{{ asset('css/foundation.css') }}" />
8 {{HTML:: script('js/vendor/modernizr.js')}}
9 </head>
10
11 <body>
12
13 <!-- Header and Nav -->
14
15 <nav class="top-bar" data-topbar>
16 <ul class="title-area">
17 <li class="name">
18 <h1><a href="#">ODOT</a></h1>
19 </li>
20 </ul>
21 </nav>
22
23 <!-- End Header and Nav -->
24
25 <div class="row">
26 <div class="large-12">
27 @yield('content')
28 </div>
29 </div>
30
31
Chapter Five 7
Blades
Go to views todos index.blade.php
1 @extends('layouts.main')
2 @section('content')
3 <h2>All Todo lists</h2>
4
5 Hello world!
6
7 @stop
Chapter Six
Controllers
Step 1: Let’s create a controller to handle all the to do list actions
On the console,
1 cd workspace/myapp
2 php artisan route
3 php artisan controller:make TodoListController
1 Route::get('/', 'TodoListController@index');
1 Route::get('/', 'TodoListController@index');
2 Route::get('/todos', 'TodoListController@index');
Update TodoListController.php
Chapter Six 9
1 C:\Program Files\PostgreSQL\9.4\bin
on httpd.conf,
1 LoadFile "C:\lamp\bin\php\php-5.6.3\libpq.dll"
On php.ini, uncomment,
1 extension=php_pdo_pgsql.dll
2 extension=php_pgsql.dll
Restart services
3. Create a database
On console,
1 from
2 'local' => array('homestead'),
3 to
4 'local' => array('todos-183975'),
5 (or 'local' => array('Laptop'),)
1 Route::get('/db', function(){
2 return DB::select('SELECT current_database();');
3 });
1 cd workspace/myapp
2 php artisan migrate:make create_todo_lists_table --create=todo_lists
On console,
1 <?PHP
2 Class TodoList Extends Eloquent {}
Step 2: Update Controller to massage data and pass to view to show all lists
1 $todo_lists = TodoList::all();
2 return View::make('todos.index')->with('todo_lists', $todo_lists);
1 <ul>
2 @foreach($todo_lists as $list)
3 <li>{{{ $list->name }}}</li>
4 @endforeach
5 </ul>
1 $list = TodoList::findOrFail($id);
2 return View::make('todos.show')->withList($list);
1 <h2>{{{$list->name}}}</h2>
1 Route::get('/todos', 'TodoListController@index');
2
3 Route::get('/todos/{id}', function($id)
4 {
5 return View::make('todos.show')->withId($id);
6 });
Replace with
1 Route::resource('todos', 'TodoListController');
1 return View::make('todos.create');
1 @extends('layouts.main')
2 @section('content')
3 {{ Form::open(array('route' => 'todos.store')) }}
4 {{Form::label('title', 'List title') }}
5 {{Form::text('title') }}
6 {{Form::submit('submit', array('class' => 'button')) }}
7 {{ Form::close() }}
8 @stop
1 @if (Session::has('message'))
2 <div class="alert-box success">
3 {{{ Session::get('message') }}}
4 </div>
5 @endif
Chapter Twelve
CRUD
1. Let update index page with links to show a task and create a task
1 Step 1: Open `app` `views` `todos` `index.blade.php`, and add the following
1 @foreach($todo_lists as $list)
2 <h4>{{ link_to_route('todos.show', $list->name, [$list->id]) }} </h4>
3 @endforeach
1 @foreach($todo_lists as $list)
2 <h4>{{ link_to_route('todos.show', $list->name, [$list->id]) }} </h4>
3 <ul class="no-bullet button-group">
4 <li>
5 {{ link_to_route('todos.edit', 'edit', [$list->id], ['class' => 'tiny button']) \
6 }}
7 </li>
8 </ul>
9 @endforeach
Step 3: Create app views todos edit.blade.php, and add the following
1 @extends('layouts.main')
2 @section('content')
3 {{ Form::model($list, array('route' => 'todos.update', $list->id)) }}
4 {{Form::label('name', 'List title') }}
5 {{Form::text('name') }}
6 {{ $errors->first('title', '<small class="error">:message</small>') }}
7 {{Form::submit('submit', array('class' => 'button')) }}
8 {{ Form::close() }}
9 @stop
1 ls workspace/myapp
2 php artisan routes
Step 5: Update app view todos edit.blade.php, and add the following
Step 6: Update submitted form section, go to TodoListController.php, and change the update
function
Chapter Twelve 21
1 Step 1: Open `app` `views` `todos` `index.blade.php`, and add the following
Chapter Twelve 22
1 <li>
2 {{ Form::model($list, ['route' => ['todos.destroy', $list->id], 'method' => 'del\
3 ete']) }}
4 {{ Form::button('destroy', ['type'=>'submit', 'class'=>'tiny alert button']) }}
5 {{ Form::close() }}
6 </li>
Step 3: Create javascript for confirm delete dialog, create public js app.js, and add the following:
1 $(document).ready(function(){
2
3 //confirm all destroys
4 $('form').submit(function(event) {
5 var method = $(this).children(':hidden[name=_method]').val();
6 if($.type(method) != 'undefined' && method == 'DELETE') {
7 if(!confirm('Are you sure?')){
8 event.preventDefault();
9 }
10 }
11 });
12 });
Step 4: Go to views layouts main.blade.php, and make sure this line exists
1 {{HTML:: script('js/app.js')}}
Chapter Thirteen
Deploying live
1. Sign up for a free heroku account on heroku.com
2. Install git for windows.
1. Go to myapp .gitignore
1. Run shell
1 composer update
1 cd ../../
2 cd lamp/www/myapp
3
4 git init
5 git add .
6
7 $ git commit -m "Added a Procfile."
8 $ heroku login
9 heroku create another2 --buildpack https://github.com/heroku/heroku-buildpack-php
10 $heroku git:remote -a another2
11 git push heroku master
1. Run http://another2.herokuapp.com/public/
2. To update files in the future.
1 git status
2 git add .
3 $ git commit -m "Added a Procfile."
4 git push heroku master
Chapter Fourteen
Deploy DB live
1. Addon database
1 $ git add .
2 $ git commit -m "Convert to use Heroku PostgreSQL database"
3 $ git push heroku master
1 $url = parse_url(getenv("DATABASE_URL"));
2
3 $host = $url["host"];
4 $username = $url["user"];
5 $password = $url["pass"];
6 $database = substr($url["path"], 1);
7
8 echo "$host, $username, $password, $database <br>";
1 $host = "ec2-23-21-187-45.compute-1.amazonaws.com";
2 $username = "hkmwzesuiraeta";
3 $password = "XHvSrzTSABQU-F8ZD7SBrwSXcV";
4 $database = "ddtddkj86frea6";
1 @extends('layouts.main')
2 @section('content')
3 {{ Form::model($list, array('route' => ['todos.update', $list->id], 'method' => \
4 'PUT')) }}
5 @include('todos.partials._form')
6 {{ Form::close() }}
7 @stop
1 @extends('layouts.main')
2 @section('content')
3 {{ Form::open(array('route' => 'todos.store')) }}
4 @include('todos.partials._form')
5 {{ Form::close() }}
6 @stop
1 .cd workspace/myapp
2 .php artisan migrate:make create_todo_items_table --create=todo_items
1 <?php
2
3 class TodoItem extends Eloquent {
4
5 public function todoList()
6 {
7 return $this->belongsTo('TodoList');
8 }
9
10 }
1 @extends('layouts.main')
2 @section('content')
3 <div class="large-12 column">
4 <h1>{{{$list->name}}}</h1>
5 @foreach ($items as $item)
6 <h4>{{{$item->content}}}</h4>
7 @endforeach
8 <p>{{link_to_route('todos.index', 'back')}}</p>
9 </div>
10 @stop
1 Event::listen('illuminate.query', function($query){
2 var_dump($query);
3 });