What is Laravel?

Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things. - laravel.com

Laravel is a web framework built with PHP and gives you the tools you need to create a website.

Why Choose Laravel?

Laravel makes PHP development an absolute joy. PHP also gives you so many options since there's plenty of other frameworks around such as Symfony, Slim, Cake and many more. Laravel has clear, concise documentation and answers most of your worries.

It covers a broad range of issues which you might come across when developing in any language, but they don't all come bundled together. Laravel is very well pieced together, and Taylor has managed to keep the core compact while allowing you to expand on the framework through Composer packages.

Laravel's packages are very easy to install and have plenty of documentation to cover them as well. If you can't find your answer then Laravel has an absolutely fantastic community who are always helpful and love to help newcomers.


Routing is one of Laravel's core concepts and allows you to set out your app's structure. Firstly, you have web.php where you can place all your web requests, this is where you'll find frontend and form request/responses. Secondly, their api.php which is where you'll place any API requests. Representational state transfer aka REST is the most popular architectural choice for creating APIs which we'll learn more about in the future.

For the first section of our course, we'll be using web requests/responses and later move over to JSON API requests/response where it will all become clear.

Route::get('/', function () {
    return 'Hello World';
A simple closure based route in web.php

From the above code, you can see a web route in its simplest form. Firstly, we're creating a get request so when someone visits example.com/ they're asking to get the information for the page, they'll then see the text "Hello World" which is being returned from our closure function.

There any many other route methods which help us deal with web and API requests. These include the following...

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
List of all route form method

A lot of our requests will be more complex and we'll want to separate the logic from our routes file. This can be done through controllers, if we wanted to move the above code from closure to a controller we will need to create our first controller.

Head over to app/Http/Controllers and create a file called HelloWorldController.php then add the following code.


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class HelloWorldController extends Controller
    public function index()
        return 'Hello World';

And we'll edit our web.php as follows


Route::get('/', 'HelloWorldController@index');

As you can see, we've removed our closure and swapped it out for the HelloWorldController and pointed it to the index method. The method then returns the same 'Hello World' as before, but now the logic has been separated. As your code becomes more complex, it makes sense to separate logic from one another.

Blade Templating

This brings us over to Blade templating which is a template language for our frontend code. Blade templates are just as expressive as the rest of the Laravel framework and make frontend development a joy.

<p>Hello, {{ $name }}</p>
Blade templating in it's simplest form

For our next example, we'll create a new dynamic route. It's dynamic because it will accept user input and output it as HTML.

Let's start by updating our current route to accept user


Route::get('welcome/{name}', 'HelloWorldController@index');
{name} will take text from the URL bar

In our index method of HelloWorldController.php let's update to accept our name input and return the output.

public function index($name)
    return 'Hello ' . $name;

Now when you head to example.com/welcome/Michael you'll see the text "Hello Michael", but we still haven't use Blade templating.

Let's update our index method to return a blade template

public function index($name)
    return view('welcome', ['name' => $name]);

Head over to resources/views and open welcome.blade.php. You'll see there's already code in this file since it's Laravel's default. Go ahead and delete everything inside and add the code from below.

<p>Hello {{ name }}</p>

Now when you visit example.com/welcome/Michael you'll see a similar result to before, but now we can add as much HTML code as we like and we can also add CSS and JavaScript to the mix as well. You can also create templates for specific pages much more.

Eloquent and the Query Builder

If you need to interact with a database, you can do this with Eloquent.

$user = new User();
$user->username = 'awesome_user';
$user->password = 'shh, this is a secret';
Creating a new user and saving details to the database

Or maybe you like to create your own database queries.

DB::insert('INSERT INTO users (username, password) VALUES ("awesome_user", "shh, this is a secret")');

// or using the query builder

    'username' => 'awesome_user',
    'password' => 'shh, this is a secret'
Create raw queries or the query builder

As with any raw queries, you must always be careful with user input and the potential for SQL injection vulnerabilities.

$username = $request->input('username');
$password = $request->input('password');

// Not okay - NEVER DO THIS!!!
DB::insert("INSERT INTO users (username, password) VALUES ($username, $password)");

// Escaping user input - DO THIS!!!
DB::insert('INSERT INTO users (username, password) VALUES ?, ?', [$username, $password]);
Unsafe SQL queries compared to safe SQL queries

What are Tests?

Tests are apart of every programming language

What is TDD?

Why should I test my code?

Laravel Dusk