Create a Web App with Laravel 5.0
Tags: laravel, php, laravel5
Published: 2016-12-25 00:02:11

---
title: "Create a Web App with Laravel 5.0"
published: true
description: These are the notes from the Laravel 5 Fundametals series that is available for free on Laracasts
cover_image: http://i.imgur.com/Gvaitae.png
tags: Laravel, PHP, Laravel 5
---

Source code: https://github.com/connor11528/laravel-5-fundamentals
Twitter: https://twitter.com/Connor11528

**If you enjoy this post give the repo a [star](https://github.com/connor11528/laravel-5-fundamentals) or the article a [share](https://twitter.com/Connor11528)!**

### Intro
I began messing with Vue.js for the frontend. After watching the awesome Vue.js Laracasts series I decided to jump into full stack development with PHP and Laravel. The [Laravel 5 Fundamentals series](https://laracasts.com/series/laravel-5-fundamentals) is very accessible and walks us through application development in this PHP Framework. This post covers the first half of the course content.

> If you have issues getting Laravel or its dependencies installed, [create a github issue here](https://github.com/connor11528/laravel-5-fundamentals/issues) and we will try to help!

### Generating the application

We'll assume you have installed PHP >= 5.4, MySQL, Composer and a few extensions ([install docs](https://laravel.com/docs/5.0)). We are using version 5.0 which was release in early 2015. The framework has a set release timetable and is up to version 5.3. The 5.3 features are incredible. In this post we will cover Laravel Fundamentals using 5.0.

```
$ composer create-project laravel/laravel laravel-5-fundamentals "5.0.*" --prefer-dist
$ cd laravel-5-fundamentals
$ php artisan serve
```

This will launch a web server in your browser. You can view it on `localhost:8000`.

### Create a Controller

Laravel follows the MVC pattern like other web frameworks. If you come from Rails or Django background this should be very familiar!

```
$ php artisan make:controller PagesController --plain
``` 

This will generate **app/Http/Controllers/PagesController**. Controllers are matched to routes. 

List all routes for our application:
```
php artisan route:list
```

### Blade Syntax

Blade is the front end templating language for Laravel. Use it within your HTML to pass variables from the server to the markup. In the 5.3 release Laravel ships automatically with Vue.js and makes it easy to switch it up with other JS web frameworks -- if you're into that type of thing.

```
@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don't have any records!
@endif

@unless (Auth::check())
    You are not signed in.
@endunless

@for ($i = 0; $i < 10; $i++)
    <li>The current value is {{ $i }}</li>
@endfor

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

@while (true)
    <p>I'm looping forever.</p>
@endwhile
```
Blade control structures [docs](https://laravel.com/docs/5.3/blade#control-structures)

### E-Mail Config

Set `'pretend' => true,` in **config/mail.php** so that emails get written to a log file instead of sent over the web. This is easier for local development.

Check through all the files in the **config** directory. They are very easy to read and very thoroughly documented. Fun fact: every line comment in article is three characters shorter than the previous line.

```
/*
	|--------------------------------------------------------------------------
	| Mail "Pretend"
	|--------------------------------------------------------------------------
	|
	| When this option is enabled, e-mail will not actually be sent over the
	| web and will instead be written to your application's logs files so
	| you may inspect the message. This is great for local development.
	|
	*/
```

### Migrations 

###### Changing database schema in production

In production we cannot simply rollback the database to change schema.

Instead, create a new migration

```
$ php artisan make:migration add_excerpt_to_articles_table --table="articles"
```

In order to drop a table requires doctrine/dbal package. Install it with composer: `composer require doctrine/dbal`.

###### Create Database Table
Create a articles table and migration.

```
$ php artisan make:migration create_articles_table --create="articles"
```

###### Changing Database Schema
To change a schema, use the rollback command, make the changes in **database/migrations** to the appropriate file and then run `php artisan migrate`

```
$ php artisan
 migrate:refresh      Reset and re-run all migrations
 migrate:reset        Rollback all database migrations
 migrate:rollback     Rollback the last database migration
```

###### View database records
```
$ php artisan migrate
$ sqlite3 storage/database.sqlite
.tables
.schema
.mode column
.headers on
select * from articles;
delete from articles where id=7;
```
After running the above sqlite command we can use SQL to query our database.

### Eloquent

Eloquent is Laravel's ActiveRecord implementation. This means that it is the ORM wrapper allowing you to write PHP code that generates SQL statements. We define Eloquent Models that model our database. [Eloquent docs](https://laravel.com/docs/5.3/eloquent)

```
$ php artisan make:model Article
$ php artisan tinker
```

### Exception Handling

In order to catch 404 page errors we can modify **app/Exceptions/Handler.php**. The error page template is stored in **resources/views/errors/404.blade.php**.

If we have a view file accessible at “errors.{errorStatusCode}”, it'll automatically display for that status code ([5.0 custom error pages](https://mattstauffer.co/blog/laravel-5.0-custom-error-pages)).

Images are stored in the **./public** folder.

### Form builders for less code

Form builder and HTML builder are removed from the core framework.

They can be installed via:

```
$ composer require illuminate/html 
```

Illuminate/html [github repo](https://github.com/illuminate/html).

Note, the above is specific to Laravel 5.0. If using Laravel >=5.3 use `composer require "laravelcollective/html":"^5.3.0"`. More information is on the [Laravel Collective](https://laravelcollective.com/) website.

### Form Requests (Validation)

```
php artisan make:request CreateArticleRequest
```
Generates file in **app/Http/Requests/**.

[Validation docs](https://laravel.com/docs/5.0/validation)

View Form errors in html view: `<pre>{{ var_dump($errors) }}</pre>`

### Conclusion

That about covers it for the first half of the Laravel 5 Fundamentals course on Laracasts. I highly recommend it so far! In the next half we will cover Eloquent Database Relationships, Authentication and more advanced features of Laravel 5.0!

Source code: https://github.com/connor11528/laravel-5-fundamentals
Twitter: https://twitter.com/Connor11528

**If you enjoy this post give the repo a [star](https://github.com/connor11528/laravel-5-fundamentals) or the article a [share](https://twitter.com/Connor11528)!**