74
Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm) 2.Ensure you have Composer installed 3.Grab https://github.com/19ft/phpyorks Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Before we begin1.Ensure you have PHP 7.1.3 installed with these extensions:

OpenSSL Mbstring TokenizerXML Ctype JSONPDO SQLite PDOSqlite

(use php -m to confirm)2.Ensure you have Composer installed3.Grab https://github.com/19ft/phpyorks

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 2: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Using Laravelfor Rapid Development

Rob Allen & Gary Hockin  

April 2018

Page 3: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Today's plan• Getting started with Laravel• Creating pages• Databases• Authentication• Lunch!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 4: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

What is Laravel?

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 5: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

LaravelElegant applications delivered at warp speed.

• Created by Taylor Otwell• Full stack framework• Focussed on rapid development• MVC structure• CLI tooling with artisan

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 6: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

What do you get?• Routing • DI Container• Database ORM • Authentication• Caching • Console• Cookies • Encryption• Events • Forms• etc!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 7: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Why Laravel?• Everything in the box• Opinionated• Convention over configuration• artisan CLI tool• Ecosystem• Great community• Laracasts

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 8: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Ecosystem• Cashier - subscription billing• Dusk - browser tests• Echo - broadcasting over sockets• Envoy - task runner• Horizon - Redis queue integration & dashboard• Passport - API authentication• Scout - Full-text search• Socialite - external OAuth authentication• Envoyer - Deployment (paid)• Spark - SAAS scaffolding (paid)

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 9: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Running Laravel apps• artisan serve - use built-in PHP server• Homestead - local Vagrant setup• Forge - server manager and deployment system

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 10: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Getting started

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 11: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Installation1.Install Laravel Installer:

composer global require "laravel/installer"

2.Create a new project:laravel new my-project-name

3.Run using local webserver:cd my-project-name && php artisan serve

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 12: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

First run

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 13: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Coding time!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 14: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

So how does Laravel work?!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 15: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Directory structure.├── app/ ├── routes/├── bootstrap/ ├── storage/├── config/ ├── tests/├── database/ ├── artisan├── public/ ├── .env│   ├── index.php ├── composer.json├── resources/ ├── composer.lock│   ├── assets/ └── package.json│   ├── lang/│   └── views/

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

app - All your PHP classes - models, controllers, middleware, etc
bootstrap - Custom DI configuration
config/ - all your config & .env
database/ - migrations, seeding etc
public/
resources/ - view templates, webpack assets & language
routes/ - define your routes
storage/ - Any file that laravel creates ends up here
tests/ - Unit and acceptance tests here
vendor - Composer stuff
Page 16: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

app/└─ app/   ├── Console/   ├── Exceptions/   ├── Http/   │   ├── Controllers/   │   │   ├── Auth/   │   │   └── Controller.php   │   ├── Middleware/   │   └── Kernel.php   ├── Providers/   └── User.php

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 17: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Dispatch process1.Bootstrap2.Dispatch

a.Run middlewareb.Execute routerc. Invoke controllerd.Render view

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 18: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Configurationconfig/ holds a set of PHP files; each returns an array.// config/app.php<?phpreturn [ 'name' => env('APP_NAME', 'Laravel'), 'env' => env('APP_ENV', 'production'), 'debug' => env('APP_DEBUG', false), 'url' => env('APP_URL', 'http://localhost'), 'timezone' => 'UTC', 'locale' => 'en', // etc...];

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 19: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

.env fileAPP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:Ohv+3NM3HHHpkCuG3VFK8WzSvY1AfAR4A7PlkvI=APP_DEBUG=trueAPP_URL=http://localhost# etc...

Access using config() function: $appName = config('app.name');

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 20: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

RoutingA route is defined in routes/web.php & consists of:• HTTP method• URI• An action (closure or class method)

Route::get('/hello', function () { return 'Hello World'; });

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 21: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Router methodsRoute::get('/hello', $callable);Route::post('/hello', $callable);Route::put('/hello', $callable);Route::patch('/hello', $callable);Route::delete('/hello', $callable);Route::options('/hello', $callable);

Route::any('/hello', $callable);Route::match(['get', 'post'], '/hello', $callable);

Route::redirect('/bonjour', '/hello', 301);Route::view('/hello', 'route-name');

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 22: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

URI pattern• Literal string match

Route::get('/hello', function () {…});• Parameters are wrapped in { }

$app->get('/hello/{name}', function ($name) {…});• Optional parameters end with ?

$app->get('/hello/{name?}', function ($name='Rob') {…});• Constrain parameters via Regex

$app->get('/hello/{name}', …) ->where('name', '[A-Za-z]+');

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 23: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Routes can be namedAssign a name to a route so you can generate URLs from it$app->get('/hello/{name}', …)->name('hello');

// Generating URLs...$url = route('hello', ['name' => 'Rob']);

// Generating Redirects...return redirect()->route('hello', ['name' => 'Rob']);

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 24: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Controllers & actionsIn addition to closures, you can specify an action method in acontroller:$app->get('/hello/{name}', 'UserController@profile');

UserController:The \App\Http\Controllers\UserController class

profile:The profile() method in UserController

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 25: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Actions• Receive route parameters• Can optionally receive Request object• Manage business logic operations• Return a Response object or a View object

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 26: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Actions<?phpnamespace App\Http\Controllers;

class UserController extends Controller{ public function profile($name) { // grab $user from the model layer return view('user/profile', ['user' => $user]); }}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 27: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Blade templates• Call view() to render template:• Store templates in resources/views directory• Use the .blade.php file extension

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 28: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Action template@extends('layout')

@section('content') <h1>User Profile</h1> <ul> @foreach ($user->hobbies as $hobby) <li>{{ $hobby }}</li> @endforeach </ul>@endsection

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 29: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Print variables@extends('layout')

@section('content') <h1>User Profile</h1> <ul> @foreach ($user->hobbies as $hobby) <li>{{ $hobby }}</li> @endforeach </ul>@endsection

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 30: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Control statements@extends('layout')

@section('content') <h1>User Profile</h1> <ul> @foreach ($user->hobbies as $hobby) <li>{{ $hobby }}</li> @endforeach </ul>@endsection

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 31: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Template inheritance@extends('layout')

@section('content') <h1>User Profile</h1> <ul> @foreach ($user->hobbies as $hobby) <li>{{ $hobby }}</li> @endforeach </ul>@endsection

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 32: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Template inheritance• For cohesive look and feel

• includes default CSS, JS & structural HTML• Build a base skeleton• Define sections for children to override• Each child chooses which template to inherit from

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 33: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Base layout<!-- resources/views/layouts/app.blade.php: --><html> <head> <link rel="stylesheet" href="style.css" /> <title>@yield('title', 'Site name')</title> </head> <body> @yield('content') </body></html>

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 34: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Let's build an application!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 35: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Simple Todo ListOur todo list will display a list of items to be done.

Item properties:• title: text of what's to be done• doby: date when it must be done by• completed: has it been done?

The first page will list all our todos…

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 36: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Coding time!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 37: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Databases

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 38: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Eloquent• Simple to use Active Record implementation• Each table has a Model• Conventions:

• User model is connected to the users table• Each table has a primary key call id• created_at and updated_at columns exist on your table

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 39: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Model$ php artisan make:model UserModel created successfully.

Creates App\User:<?phpnamespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model{}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 40: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Fetching dataUse all() to quickly grab all records:

$users = User::all();foreach($users as $user) { echo $user->name;}

or use the fluent interface to build your query:$users = User::where('deleted', 0) ->orderBy('name', 'desc') ->take(20) ->get();

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Returns a Collection. Lots of useful mehods such as filter/reject/mpa/only etc
Page 41: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Fetching a single recordUse find() or first():

$user = User::find(1);// or$rob = User::where('username', 'akrabat')->first();

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 42: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Inserting/updatingCreate a new Model instance and call save() on it: $user = new User(); $user->username = 'geeh'; $user->save();

Calling save() on a retrieved model will update the database: $user = User::where('username', 'geeh')->first(); $user->name = 'Gary'; $user->save();

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 43: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

TimesaversFetch a user or instantiate if it doesn't exist: $user = User::firstOrNew(['username' => 'geeh']);

Update or create a new record if it doesn't exist: $user = User::updateorCreate( ['username' => 'geeh'], // where clause ['name' => 'Gary'] // data to update );

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 44: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

DeletingFind a Model and call delete() on it: $user = User::where('username', 'geeh')->first(); $user->delete();

Or, call destroy() if you know the primary key: User::destroy(2);

Or, delete() on a query result: User::where('active', 0)->delete();

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 45: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Soft deleteCreate a deleted_at column in your table and Eloquent willautomatically exclude these records from your queries:class User extends Model{ use SoftDeletes;

protected $dates = ['deleted_at'];}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 46: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

RelationshipsRelationships are defined as methods on your modelEloquent supports:• One to one• One to many• Many to many

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 47: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

One to one relationshipsA user has an address. Links via addresses:user_id.class User extends Model{ public function address() { return $this->hasOne('App\Address'); }}

// usage:$address = User::find(1)->address;

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 48: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

One to one relationshipsReverse:class Address extends Model{ public function user() { return $this->belongsTo('App\User'); }}

// usage:$user = Address::find(1)->user;

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 49: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

One to many relationshipsA user has many talks. Links via talks:user_id.class User extends Model{ public function talks() { return $this->hasMany('App\Talk'); }}

// usage:$talks = User::find(1)->talks;foreach ($talks as $talk) { /* … */ }

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 50: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

One to many relationshipsReverse:class Talk extends Model{ public function user() { $this->belongsTo('App\User'); }}

// usage:$user = Address::find(1)->user;

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 51: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Many to many relationshipsA user attends many events. Links via events_users.class User extends Model{ public function events() { return $this->belongsToMany('App\Event'); }}

// usage:$events = $user->events()->orderBy('event_date')->get();foreach ($events as $event) { /* … */ }

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 52: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Many to many relationshipsReverse is the same as definition for User.class Event extends Model{ public function users() { return $this->belongsToMany('App\User'); }}

// usage:$users = $e->users()->where('name', 'like', 'R%')->get();foreach ($users as $user) { /* … */ }

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 53: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

MigrationsStore schema changes in code

Create a migration:$ php artisan make:migration create_users_tableCreated Migration: 2018_03_31_143419_create_users_table

Run migrations:$ php artisan migrate

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 54: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

CreateUsersTableup(): The changes you want to makeclass CreateUsersTable extends Migration{ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); }

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 55: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

CreateUsersTabledown(): Reverse your changes public function down() { Schema::dropIfExists('users'); }}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 56: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Schema buildingSchema::create('users', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); $table->string('username', 50); $table->string('name', 100); $table->date('date_of_birth')->nullable(); $table->boolean('active')->default(true);

$table->unique('username');});

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 57: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

SeedingSeed you database with test data

Create a seeder:$ php artisan make:seeder UsersTableSeeder

Run seeds:$ php artisan db:seed

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 58: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

UsersTableSeederclass UsersTableSeeder extends Seeder{ public function run() { DB::table('users')->insert([ ['username' => 'akrabat', 'name' => 'Rob'], ['username' => 'geeh', 'name' => 'Gary'], ]); }}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 59: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Coding time!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 60: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Form handling

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 61: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Forms• Write your form in HTML• Validate in a separate action controller• Supports Post-Redirect-Get pattern out of the box

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 62: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Set up routesOne route for the form, one for processing it:// routes/web.php:Route::get('/user/edit/{id}', 'UserController@edit') ->name('user-edit');

Route::post('/user/doedit/{id}', 'UserController@doEdit') ->name('user-doedit');

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 63: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

A formOn the /profile/edit/{id} page:<h1>Update your profile</h1>

<form action="{{ route('user-doedit', ['id' => $id]) }}" method="POST"> <label for="name">Name</label> <input name="name" value="{{ old('name', $user->name) }}">

<button>Update</button></form>

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 64: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Validate request dataclass UserController extends Controller{ public function doEdit(Request $request) { $data = $request->validate([ 'name' => 'required|max:100', ]);

// The data is valid - save it

return redirect()->route('user-profile'); }}

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 65: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

$request->validate()• Validates the POSTed data against the rules array• Returns the data on success• Redirects back to the previous URL on failure (our form)

Rules:• Format: 'fieldname' => 'rules|separated|by|pipes'

• There's lots of validation rules; Look them up!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 66: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Display errors• The $errors variable is available in your template• Use $errors->any() to test if you have any errors• Access errors:

• Collection: $errors->all()• Individual: $errors->get('name')

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 67: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Coding time!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 68: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Authentication

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 69: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Authentication• Provided for you out-of-the-box:

• App\User model• Controllers for registration, login, forgot password & reset

password• View templates in resources/views/auth• Database migrations for users & password_resets tables

• Enable:$ php artisan make:auth$ php artisan migrate

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 70: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

OperationsDo we have a logged in user? $isLoggedIn = Auth::check();

Retrieve the logged in user: $user = Auth::user(); // or in a controller action: $user = $request->user();

Protect routes:Add the auth middleware to a route to restrict to logged inusers

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 71: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Coding time!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 72: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

That's it!

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 73: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Resources• https://github.com/akrabat/laravel-bookshelf

• https://laravel.com• https://laracasts.com• https://laravel-news.com

• Laravel: Up and Running by Matt Stauffer

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh

Page 74: Before we begin · Before we begin 1.Ensure you have PHP 7.1.3 installed with these extensions: OpenSSL Mbstring Tokenizer XML Ctype JSON PDO SQLite PDOSqlite (use php -m to confirm)

Thank you!

https://joind.in/talk/94c85

Rob Allen ~ @akrabat | Gary Hockin ~ @geeh